Java 如何使用比较器对 ArrayList 进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22314009/
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
How to sort ArrayList using Comparator?
提问by Reeggiie
I have a Class Student that Implements a static method
我有一个实现静态方法的类学生
public static Comparator<Student> getCompByName()
that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'.
为 Student 返回一个新的比较器对象,该对象通过属性“名称”比较 2 个学生对象。
I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName().
我现在需要通过使用我的函数 getCompByName() 按“姓名”对学生 ArrayList 进行排序来测试这一点。
Here is my Comparator method in my Student class.
这是我的 Student 类中的 Comparator 方法。
public static Comparator<Student> getCompByName()
{
Comparator comp = new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2)
{
return s1.name.compareTo(s2.name);
}
};
return comp;
}
And Main where I need to test
我需要测试的主要地方
public static void main(String[] args)
{
// TODO code application logic here
//--------Student Class Test-------------------------------------------
ArrayList<Student> students = new ArrayList();
Student s1 = new Student("Mike");
Student s2 = new Student("Hector");
Student s3 = new Student("Reggie");
Student s4 = new Student("zark");
students.add(s1);
students.add(s2);
students.add(s3);
students.add(S4);
//Use getCompByName() from Student class to sort students
Can anyone show me how I would use the getCompByName() in my main to actually sort the ArrayList by name? Im new to comparators and having a hard time with their usages. The method returns a comparator, so Im not sure how this will be implemented. I know I need to use getCompByName() to sort, im just not sure how to implement it.
任何人都可以告诉我我将如何使用我的主文件中的 getCompByName() 来实际按名称对 ArrayList 进行排序?我是比较器的新手,很难使用它们。该方法返回一个比较器,所以我不确定这将如何实现。我知道我需要使用 getCompByName() 进行排序,我只是不确定如何实现它。
采纳答案by Kevin Bowersox
Use the Collections.sort(List, Comparator)method:
使用Collections.sort(List, Comparator)方法:
Collections.sort(students, Student.getCompByName());
Also in your code it would be good to use the List
interface when declaring the List
:
同样在您的代码中,List
在声明以下内容时最好使用该接口List
:
List<Student> students = new ArrayList();
You could also tighten up the code by using a Student[]
and passing it to the ArrayList
constructor:
您还可以通过使用 aStudent[]
并将其传递给ArrayList
构造函数来收紧代码:
public static void main(String[] args) {
Student[] studentArr = new Student[]{new Student("Mike"),new Student("Hector"), new Student("Reggie"),new Student("zark")};
List<Student> students = new ArrayList<Student>(Arrays.asList(studentArr));
Collections.sort(students, Student.getCompByName());
for(Student student:students){
System.out.println(student.getName());
}
}
Here is a Gist of the full source.
回答by fge
Use Collections.sort()
:
Collections.sort(students, getCompByName());
Note: might be useful to make your comparator a private static final
variable.
注意:可能有助于使您的比较器成为private static final
变量。
Note 2: modifies the list in place; does not create a new list.
注2:就地修改列表;不会创建新列表。