Tuesday, November 28, 2017

JAVA - How to sort array elements in descending order

For sorting array elements use class java.util.Arrays. This class contains static methods for work with arrays.
String[] array = { "One", "Two", "Three", "Four", "Fight" };

Arrays.sort( array, Collections.reverseOrder() );

System.out.println( Arrays.toString( array ) );
For descending sorting use comparator Collections.reverseOrder().

Output is:
[Two, Three, One, Four, Fight]

No comments:

Post a Comment