8

在我使用的代码下面可以正常工作并输出名称,但排序方法不起作用。我期待“Collections.sort(nameFromText);” 按名字的字母顺序对 ArrayList 进行排序。

我究竟做错了什么?

public static void main(String[] args) throws IOException {
    // Create and populate text file
    Writer textFile = new FileWriter("names.txt");
    String[] nameArray = new String[] { "Tina Tully\n", "Bill Simpson\n",
            "Dana Smith\n", "Ralph Andrews\n", "Greg Smithers\n",
            "Lisa Krump\n", "Gill Bitters\n", "Barbara West\n",
            "Sandra McDonald\n", "Bart Willis\n", "Bucky Zimmerman\n",
            "Richard Vicks\n", "Velma Tarp\n", "Winslow Tunnell\n",
            "Andrew Letterman\n", "Betty Trump\n", "Waldo Smith\n",
            "Kyle Ronno\n", "Vivian West\n", "Wendy Tunnell\n" };
    generateText(textFile, nameArray);

    // Create object of previously created text file
    Scanner pullFile = new Scanner(new File("names.txt"));

    // Create 20 Person Objects and add to ArrayList data structure with
    // name variables assigned to values from text file
    ArrayList<Person> nameFromText = new ArrayList<Person>();
    fillArrayList(nameFromText, pullFile);

    // Sort ArrayList
    Collections.sort(nameFromText);

    // Print ArrayList
    printNamesFromObjects(nameFromText);
}

private static void printNamesFromObjects(ArrayList<Person> namesFromText) {
    for (int i = 0; i < 20; i++) {
        System.out.println(namesFromText.get(i).name);
    }
}

private static void fillArrayList(ArrayList<Person> nameFromText,
        Scanner pullFile) {
    while (pullFile.hasNext()) {
        Person obj = new Person(pullFile.nextLine());
        nameFromText.add(obj);
    }
}

private static void generateText(Writer textFile, String[] nameArray)
        throws IOException {
    for (int i = 0; i < 20; i++) {
        textFile.write(new String(nameArray[i]));
    }
    textFile.close();
}
4

5 回答 5

28

Collections.sort(List<T>)方法期望它正在排序的列表元素具有可比性。元素类型T应该实现接口,或者您应该使用采用泛型实例Comparable的重载sort()方法。Comparator

在下面的代码中,您不满足上述两个条件。你的Person类都没有实现Comparable,你也没有传递任何Comparator实例。

ArrayList<Person> nameFromText = new ArrayList<Person>();
fillArrayList(nameFromText, pullFile);
// Sort ArrayList
Collections.sort(nameFromText);  // How to sort?

Comparator您应该为您的类创建一个Person来告诉sort()方法如何对其进行排序(可能在存储在 Person 类中的 String 上)

以下是实现通用比较器的方法:

public class PersonNameComparator implements Comparator<Person> {
    public int compare(Person p1, Person p2) {
        return p1.getName().compareTo(p2.getName());
    }
}

然后你的Collections.sort()方法调用应该是这样的: -

Collections.sort(nameFromText, new PersonNameComparator());
于 2012-10-07T18:28:31.617 回答
4

或者,您可以直接在 Person 类中实现 Comparable 接口并覆盖“compareTo(Object obj)”方法。在这种情况下,您不需要为比较器创建新类。这就像内置排序。

于 2012-10-07T18:45:45.003 回答
2

尝试这个:

List<String> inputString =  Arrays.asList("Sijan", "Sudeep", "Parasar", "Raj Kumar");
Collections.sort(inputString);
System.out.println(inputString);
于 2013-08-26T18:57:41.553 回答
1

如果您使用 Java 8,则使用一个线性Lambda 表达式

ArrayList<Person> nameFromText = new ArrayList<Person>();
fillArrayList(nameFromText, pullFile);
nameFromText.sort((p1, p2) -> p1.getName().compareTo(p2.getName()));
于 2019-01-09T12:52:44.157 回答
0

use Collections.sort(managerNameList);

ArrayList<String> managerNameList = new ArrayList<String>();

    managerNameList.add("antesh");
    managerNameList.add("Lalit");
    managerNameList.add("Gokul");
    managerNameList.add("Ajay");

    System.out.println("Arraylist before sorting");    
    for(String name: managerNameList)
    {    
        System.out.println(name);           
    }

    Collections.sort(managerNameList);

    System.out.println("Arraylist after sorting");    
    for(String name: managerNameList)
    {    
        System.out.println(name);           
    }
于 2013-11-21T06:22:49.560 回答