Write a program in java language to illustrate reverse sort of an array.The program should sort them in natural, alphabetical order using the default version...

      

Write a program in java language to illustrate reverse sort of an array.The program should sort them in natural, alphabetical order using the default version of sort();

  

Answers


Davis
//Sort an array of string in reverse order.
Import java.until *;
//Create a Comparator that returns the outcome
//of a reverse string comparison.
Class RevStrComp implements Comprator {
//Implement the compare () method so that it
//reverses the order of the string comparison.
publicint compare(String strA, strB) {
//ComparestrB to strA, rather than strA to strB.
returnstrB.compareTo(strA);
}
}
//Demonstrate the reverse string comparator.
Class RevStrSort {
Public static void main(String[] args);{
//Create a sample array of strings
String strs[] = { “dog”, “horse”, “zebra”, “cow”, “cat” };
//show the initial order.
System.out.print(“Initial order: ‘’);
for (String s : strs)
System.out.print (s + “”);
System.out.println(‘’\n’’);
//Sort the array in reverse order
//Begin by creating a reverse string comparator.
RevStrComprsc = new RevStrComp();
//Now sort the string using the reverse comparator
Arrays.sort (strs, rsc);
//Show the reverse sorted order.
System.out.print (Sorted in reverse order: ‘’);
For(String s : strs)
System.out.print(s + ‘’ ‘’);
System.out.println(‘’\n’’);
//For comparison, sort the strings in natural order.
Arrays.Sort(strs);
//Show the natural sorted order.
System.out.print(‘’Sorted in natural order: ‘’);
for (String s : strs)
System.out.print(s + ‘’ ‘’);
System.out.println(‘’\n’’);
}
}

Githiari answered the question on January 26, 2018 at 12:11


Next: State TWO main ways through which data is accessed in a storage media and give an example of a storage media in each case.
Previous: Define the term compiler as used in programming

View More Computer Studies Questions and Answers | Return to Questions Index


Exams With Marking Schemes

Related Questions