按字母顺序列出字符串数组

我有一个程序,用户输入一个名称列表。 我有一个开关案例转到一个函数,我希望按字母顺序打印名称。

public static void orderedGuests(String[] hotel) { //?? } 

我试过了两个

 Arrays.sort(hotel); System.out.println(Arrays.toString(hotel)); 

 java.util.Collections.sort(hotel); 

很奇怪,你的代码似乎对我有用:

 import java.util.Arrays; public class Test { public static void main(String[] args) { // args is the list of guests Arrays.sort(args); for(int i = 0; i < args.length; i++) System.out.println(args[i]); } } 

我使用“java Test Bobby Joe Angel”运行该代码,这是输出:

 $ java Test Bobby Joe Angel Angel Bobby Joe 

你试过的第一件事似乎工作得很好。 这是一个示例程序。
按下本页顶部的“开始”按钮运行它以自己查看输出。

 import java.util.Arrays; public class Foo{ public static void main(String[] args) { String [] stringArray = {"ab", "aB", "c", "0", "2", "1Ad", "a10"}; orderedGuests(stringArray); } public static void orderedGuests(String[] hotel) { Arrays.sort(hotel); System.out.println(Arrays.toString(hotel)); } } 

你可以使用Arrays#sort() ,它工作得很好。 看这个例子:

 String [] a = {"English","German","Italian","Korean","Blablablabla.."}; //before sort for(int i = 0;i 

这是有效的代码:

 import java.util.Arrays; import java.util.Collections; public class Test { public static void main(String[] args) { orderedGuests1(new String[] { "c", "a", "b" }); orderedGuests2(new String[] { "c", "a", "b" }); } public static void orderedGuests1(String[] hotel) { Arrays.sort(hotel); System.out.println(Arrays.toString(hotel)); } public static void orderedGuests2(String[] hotel) { Collections.sort(Arrays.asList(hotel)); System.out.println(Arrays.toString(hotel)); } } 

java.util.Collections.sort(listOfCountryNames,Collat​​or.getInstance());

按字母顺序我假设顺序是:A | a

我建议有一个Comparator-interface的类实现比较方法:

 public int compare(Object o1, Object o2) { return o1.toString().compareToIgnoreCase(o2.toString()); } 

从调用方法调用带有自定义Comparator的Arrays.sort方法:

 Arrays.sort(inputArray, customComparator); 

观察结果:输入数组:“Vani”,“Kali”,“Mohan”,“Soni”,“kuldeep”,“Arun”

输出(按字母顺序排列)是:Arun,Kali,kuldeep,Mohan,Soni,Vani

输出(通过执行Arrays.sort(inputArray)的自然顺序是:Arun,Kali,Mohan,Soni,Vani,kuldeep

因此,在自然排序的情况下,[Vani

有关自然和字母/词汇顺序的更多理解,请访问此处

 **//With the help of this code u not just sort the arrays in alphabetical order but also can take string from user or console or keyboard import java.util.Scanner; import java.util.Arrays; public class ReadName { final static int ARRAY_ELEMENTS = 3; public static void main(String[] args) { String[] theNames = new String[5]; Scanner keyboard = new Scanner(System.in); System.out.println("Enter the names: "); for (int i=0;i 

Arrays.sort(字符串数组); 这将根据Unicode字符值对字符串数组进行排序。 在字符串开头包含大写字符的所有字符串将按字母顺序排在排序列表的顶部,后跟所有带小写字符的字符串。 因此,如果数组包含以大写字符和小写字符开头的字符串,则排序的数组不会返回不区分大小写的顺序字母顺序列表

 String[] strArray = { "Carol", "bob", "Alice" }; Arrays.sort(strList); System.out.println(Arrays.toString(hotel)); 

输出是:Alice,Carol,bob,

如果你需要对字符串进行排序而不考虑大小写,那么你需要为Arrays.sort()提供第二个参数,一个Comparator。 这样的比较器已经为我们编写,可以在名为CASE_INSENSITIVE_ORDER的String类中作为静态访问。

 String[] strArray = { "Carol", "bob", "Alice" }; Arrays.sort(stringArray, String.CASE_INSENSITIVE_ORDER); System.out.println(Arrays.toString(strArray )); 

输出是:Alice,bob,Carol

您可以使用Arrays.sort()方法。 这是一个例子,

 import java.util.Arrays; public class Test { public static void main(String[] args) { String arrString[] = { "peter", "taylor", "brooke", "frederick", "cameron" }; orderedGuests(arrString); } public static void orderedGuests(String[] hotel) { Arrays.sort(hotel); System.out.println(Arrays.toString(hotel)); } } 

产量

[布鲁克,卡梅伦,弗雷德里克,皮特,泰勒]