按字典顺序对字符串数组进行排序

时间:2015-01-17 20:14:40

标签: java sorting

我在Java中有一个字符串数组。我如何按字典顺序排序? Collection.sortArrays.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

3 个答案:

答案 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