0

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
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
user4447943
  • 295
  • 1
  • 4
  • 11

3 Answers3

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

reverse:

String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" };
Arrays.sort(strings, Collections.reverseOrder());
roeygol
  • 4,908
  • 9
  • 51
  • 88
1
String[] a = { "BANANA", "ANANA", "NANA", "ANA", "NA", "A" };
Arrays.sort(a);
bhspencer
  • 13,086
  • 5
  • 35
  • 44
-2

You have just to look at the javaDocs API references for how to sort arrays. http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

helTech
  • 95
  • 1
  • 1
  • 7