Write Java code to sort the given array in reverse alphabetical order and return the position of the given string in the array. The return type of the output is an integer which is the position of a given string value from the array.
Input and Output Format
- Input is a string array. The first element in the input represents the size of the array. Assume the position of the first element is 1.
- The output is an integer which is the position of the string variable.
Sample Input 1:
4
red
green
blue
ivory
ivory
Sample Output 1:
2
Sample Input 2:
3
grape
mango
apple
apple
Sample Output 2:
3
Find the element position in a reversed string array in Java
Following are the steps to find the position of an element in a reversed string.
- Input the size of an array.
- Create an array of strings.
- Add an element to the array.
- Now, take input whose position we have to find. And, then pass both the array and element to getvalues() method.
- Inside the method, create an empty array list. Add, element to the list and perform the below-mentioned operation.
- First, sort the list using Collections.sort().
- Then, reverse it using Collections.reverse() method.
- Now, check the position of the element that we need to find by iterating the list.
- If found just display the index of the element.
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int fr=sc.nextInt(); String a[]=new String[fr]; for(int i=0;i<fr;i++) { a[i]=sc.next(); } String ba=sc.next(); getvalues(a,ba); } public static void getvalues(String[] a, String b) { ArrayList<String>al=new ArrayList<String>(); for(int i=0;i<a.length;i++) { al.add(a[i]); } System.out.println(al); Collections.sort(al); System.out.println(al); Collections.reverse(al); System.out.println(al); for(int i=0;i<al.size();i++) { if(b.equals(al.get(i))) { System.out.println(i+1); } } } }
Output
Reverse & Sort elements without sort() and reverse() method
Write Java code to sort the given array in reverse alphabetical order. Here, we don’t have to use the sort() and reverse() method of Collection.
Input and Output Format
- Input is a string array. The first element in the input represents the size of the array. Assume the position of the first element is 1.
- The output is sorted and reversed in an array.
Sample Input 1:
3
Ron
Harry
Hermoine
Sample Output 1:
Strings after sorting
Harry
Hermoine
Ron
String after reverse
Ron
Hermoine
Harry
Java Programs to reverse and sort Array of string
Following are the steps to sort and reverse arrays of string:
- Input the size of an array.
- Create an array of strings
- Add elements to an array.
- Pass both the size and array to the getvalues() method.
- Inside the method, sort the string in an array by comparing and swapping the element with its next element. Then, print the sorted form of an array.
- Next, loop through the sorted array and reverse them, and at last print it.
package com.company; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Solution16 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int fr = sc.nextInt(); String a[] = new String[fr]; for (int i = 0; i < fr; i++) { a[i] = sc.next(); } getvalues(a, fr); } public static void getvalues(String[] a, int fr) { //sort string for(int i =0 ;i < fr;i++){ for(int j = i+1; j < fr ; j++ ){ if(a[i].compareTo(a[j])>0) { String temp = a[i]; a[i] = a[j]; a[j] = temp; } } } //String after sorting System.out.println("Strings after sorting"); for(int i = 0;i <= fr-1;i++) { System.out.println(a[i]); } // Now we will reverse System.out.println("String after reverse"); for(int i = fr-1; i >=0 ;i--){ System.out.println(a[i]); } } }
Output
Thus, in this way, we wrote a Java program to Find the element position in a reversed string array. As well as how to sort and reverse array without sort() and reverse() method.