如何对 class 对象的 ArrayList 的一部分进行排序?

How to sort parts of an ArrayList of objects of a class?

我看过许多看似相似的问题,但其中 none 完全解决了我的问题。

我有一个名为 Student 的 class,一个名为 RollManager 的 class,以及一个名为 RollDriver 的驱动程序。 Student class 允许用户输入学生的数据,例如他们的姓名、专业、GPA、class学历等

RollManager class 有一个名为 classRoll 的 ArrayList,其中包含 Student 类型的对象(看起来像这样:ArrayList<Student> classRoll = new ArrayList<>();

在 RollDriver 中有一个菜单,允许用户做几件事。其中,我需要能够按名称对学生对象进行排序,以及允许我按 GPA 对它们进行排序的单独选项。

问题是当我尝试使用 Collections.sort(classRoll 时,它不知道如何对它们进行排序。所以我在 RollManager class 中创建了一个名为 sortName 的方法,但是我如何指定我想专门根据 Student 对象的 "name" 值进行排序?这是我的一些代码:

滚动管理器:

public static void sortName(ArrayList<Student> classRoll)
    {
        for(int i = 0; i < classRoll.size(); i++)
        {
            int pos = i;
            for(int n = i; n < classRoll.size(); n++)
            {
                if(classRoll.get(n).equals(classRoll.get(pos)))
                {
                    pos = n;
                }
            }
        }
    }

RollDriver 中用于排序的选项:

else if(choice == 8)
            {
                RollManager.sortName(classRoll);
                System.out.println (classRoll);
            }

当我 运行 这样做时,我没有收到任何错误,但也没有任何反应。对象的排序没有任何不同。

这些是我添加到 classRoll 以测试我的代码的一些预添加对象(class化是一个枚举):

Student student2 = new Student("John", "Doe", "COMM", 120, 3.65, Classification.FRESHMAN);
Student student3 = new Student("Bob", "Ross", "ARTS", 200, 3.99, Classification.OTHER);
Student student4 = new Student("Liev", "Schreiber", "FILM", 100, 2.53, Classification.GRADUATE);
Student student5 = new Student("Maury", "Povich", "PSCI", 75, 2.24, Classification.JUNIOR);
Student student6 = new Student("Bill", "Leidermann", "CSCI", 90, 2.95, Classification.SENIOR);

classRoll.add (student2);
classRoll.add (student3);
classRoll.add (student4);
classRoll.add (student5);
classRoll.add (student6);

我希望这些信息足够了。如有必要,我可以 post 更多我的代码。感谢您的帮助!

您使用另一种 Collections.sort 方法:sort(List<T> list, Comparator<? super T> c)

你这样使用它:

Collections.sort(classRoll, new Comparator<Student>() {
    @Override
    public int compare(Student s1, Student s2) {
        return s1.getName().compareTo(s2.getName());
    }
});

在Java 8+更简单:

// Sort by name
classRoll.sort(Comparator.comparing(Student::getName));

// Sort by GPA
classRoll.sort(Comparator.comparingDouble(Student::getGpa));

如果 name 是 2 个字段(firstNamelastName),您可以使用 thenComparing:

// Sort by last name, then first name
classRoll.sort(Comparator.comparing(Student::getLastName)
                         .thenComparing(Student::getFirstName));

执行此操作的方法是将 Collections.sort 与自定义比较器一起使用:

Comparator<Student> nameComparator = new Comparator<Student>() {
     public int compare(Student student1, Student student2) {
        return student1.getName().compareTo(student2.getName());
     }
  };

Comparator<Student> gpaComparator = new Comparator<Student>() {
     public int compare(Student student1, Student student2) {
        return student1.getGPA().compareTo(student2.getGPA());
     }
  };

然后你可以根据需要使用不同的比较器:

Collections.sort(classRoll, nameComparator);

与上面的 2 个循环的解决方案相比,这种方式可以优化排序器。