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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:26:19  来源:igfitidea点击:

Sort the array of string Lexicographically

javasorting

提问by user4447943

I have an array of string in Java. How do I sort it lexicographically? Is there any built-in function in Collection.sortor Arrays.sort?

我在 Java 中有一个字符串数组。我如何按字典顺序对其进行排序?是否有任何内置功能Collection.sortArrays.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