我在Java中有一个字符串数组。我如何按字典顺序排序? Collection.sort
或Arrays.sort
中是否有内置函数?
例如:
0) BANANA
1) ANANA
2) NANA
3) ANA
4) NA
5) A
排序后:
5) A
3) ANA
1) ANANA
0) BANANA
4) NA
2) NANA
答案 0 :(得分:3)
String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" };
Arrays.sort(strings);
反向:
String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" };
Arrays.sort(strings, Collections.reverseOrder());
答案 1 :(得分:1)
String[] a = { "BANANA", "ANANA", "NANA", "ANA", "NA", "A" };
Arrays.sort(a);
答案 2 :(得分:-1)
您只需查看javaDocs API参考,了解如何对数组进行排序。 http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html