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

29
我有一个程序,用户可以输入一个名字列表。我使用了一个开关语句调用一个函数,想要让这些名字按照字母顺序打印出来。
public static void orderedGuests(String[] hotel)
{
  //??
}

我已经尝试过两种方法。

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

java.util.Collections.sort(hotel);

3
那些解决方案有什么问题? - Brendan Long
发布整个代码。按照你进行每一步的顺序。 - Maroun
10个回答

29

奇怪,你的代码在我这里似乎可以工作:

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

5

Arrays.sort(stringArray); 这个方法基于Unicode字符的值来对字符串数组进行排序。所有以大写字母开头的字符串将按字母表顺序排在排序列表的顶部,接着是所有以小写字母开头的字符串。 因此,如果数组包含以大写字母和小写字母开头的字符串,排序后的数组不会返回一个大小写不敏感的字母表顺序列表。

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

输出结果为:Alice,Carol,bob。

如果您需要按照不区分大小写的方式对字符串进行排序,则需要为Arrays.sort()提供第二个参数Comparator。这样的比较器已经为我们编写好,并且可以作为String类的静态属性CASE_INSENSITIVE_ORDER来访问。

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

输出为:Alice,bob,Carol


5
你尝试的第一件事似乎很好用。 下面是一个示例程序。
请按此页面上方的“开始”按钮运行它以查看输出结果。
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));
    }
}

3
你可以直接使用Arrays#sort(),它能完美地工作。看这个例子:
String [] a = {"English","German","Italian","Korean","Blablablabla.."};
//before sort
for(int i = 0;i<a.length;i++)
{
  System.out.println(a[i]);
}
Arrays.sort(a);
System.out.println("After sort :");
for(int i = 0;i<a.length;i++)
{
  System.out.println(a[i]);
}

2
java.util.Collections.sort(listOfCountryNames, Collator.getInstance());

什么是Collator? - CiY3

1
这是可用的代码:
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));
    }

}

1

CompareTo()方法:基于Unicode字符值比较两个字符串。

import java.util.*;

public class Test {

int n,i,temp;
String names[n];

public static void main(String[] args) {
String names[5] = {"Brian","Joshua","Louis","David","Marcus"};

for(i=0;i<5;i++){
    for(j=i+1;i<n;j++){
        if(names[i].CompareTo(names[j]>0) {
             temp=names[i];
             names[i]=names[j];
             names[j]=temp;
         } else
             System.out.println("Alphabetically Ordered");                               
         }
     }                              
}

1
按字母顺序排序,我认为顺序应该是:A|a < B|b < C|c... 希望这正是@Nick正在寻找的,并且答案遵循上述假设。
我建议使用一个类来实现Comparator接口的比较方法:
public int compare(Object o1, Object o2) {
    return o1.toString().compareToIgnoreCase(o2.toString());
}

并且从调用方法中使用自定义比较器调用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 < kuldeep],这不符合我对字母顺序的理解。
要了解更多有关自然和字母/词汇顺序的信息,请访问此处的讨论

0
**//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<theNames.length ;i++ )
{           
theNames[i] = keyboard.nextLine();
}
System.out.println("**********************");
Arrays.sort(theNames);
for (int i=0;i<theNames.length ;i++ )
{
System.out.println("Name are " + theNames[i]);
}
}
}**

0
 public static String[] textSort(String[] words) {
    for (int i = 0; i < words.length; i++) {
        for (int j = i + 1; j < words.length; j++) {
            if (words[i].compareTo(words[j]) > 0) {
                String temp = words[i];
                words[i] = words[j];
                words[j] = temp;
            }
        }
    }

    return words;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接