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
  • 14,339
  • 1
  • 33
  • 57
user4447943
  • 295
  • 1
  • 3
  • 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,562
  • 7
  • 46
  • 82
1
String[] a = { "BANANA", "ANANA", "NANA", "ANA", "NA", "A" };
Arrays.sort(a);
bhspencer
  • 12,235
  • 4
  • 30
  • 41
-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