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 13 Answers
String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" }; Arrays.sort(strings); reverse:
String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" }; Arrays.sort(strings, Collections.reverseOrder()); 4String[] a = { "BANANA", "ANANA", "NANA", "ANA", "NA", "A" }; Arrays.sort(a); 1You have just to look at the javaDocs API references for how to sort arrays.