I have an array of string in Java. How do I sort it lexicographically? Is there any built-in function in Collection.sort or Arrays.sort?

For example:

0) BANANA 1) ANANA 2) NANA 3) ANA 4) NA 5) A 

after sorting:

5) A 3) ANA 1) ANANA 0) BANANA 4) NA 2) NANA 
1

3 Answers

String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" }; Arrays.sort(strings); 

reverse:

String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" }; Arrays.sort(strings, Collections.reverseOrder()); 
4
String[] a = { "BANANA", "ANANA", "NANA", "ANA", "NA", "A" }; Arrays.sort(a); 
1

You have just to look at the javaDocs API references for how to sort arrays.