Java 按字典顺序对字符串数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28003847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort the array of string Lexicographically
提问by user4447943
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
?
我在 Java 中有一个字符串数组。我如何按字典顺序对其进行排序?是否有任何内置功能Collection.sort
或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
回答by roeygol
String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" };
Arrays.sort(strings);
reverse:
逆转:
String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" };
Arrays.sort(strings, Collections.reverseOrder());
回答by bhspencer
String[] a = { "BANANA", "ANANA", "NANA", "ANA", "NA", "A" };
Arrays.sort(a);
回答by helTech
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
您只需查看 javaDocs API 参考以了解如何对数组进行排序。 http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html