java 在 int 属性上按降序对自定义对象数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15326248/
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
Sort an array of custom objects in descending order on an int property
提问by PrimalScientist
I would like to sort my array in descending order by year of birth. My array has two other elements which are of type String. So, as an example the person who was born in the earliest year, such as 1939, would be at the top, then so on.
我想按出生年份降序对数组进行排序。我的数组还有另外两个字符串类型的元素。因此,例如,出生在最早年份(例如 1939 年)的人将位于顶部,然后依此类推。
Here is my code:
这是我的代码:
import java.util.*;
public class StudentInformationTest
{
public static void main (String [] args){
StudentInformation[] studentInfo = new StudentInformation[10];
studentInfo[0] = new StudentInformation("Student A",1971, "BSc FDIT");
studentInfo[1] = new StudentInformation("Student B",1964, "BSc FDIT");
studentInfo[2] = new StudentInformation("Student C",1996, "BSc FDIT");
studentInfo[3] = new StudentInformation("Student D",1939, "BSc FDIT");
studentInfo[4] = new StudentInformation("Student E",1945, "BSc FDIT");
studentInfo[5] = new StudentInformation("Student F",1991, "BSc FDIT");
studentInfo[6] = new StudentInformation("Student G",1987, "BSc FDIT");
studentInfo[7] = new StudentInformation("Student H",1968, "BSc FDIT");
studentInfo[8] = new StudentInformation("Student I",1968, "BSc FDIT");
studentInfo[9] = new StudentInformation("Student J",1973, "BSc FDIT");
printInfo(studentInfo);
printAge(studentInfo);
}
public static void printInfo(StudentInformation studentInfo[]){
for(int i = 0; i < studentInfo.length; i++){
System.out.println(studentInfo[i].getStudentName() + " " + studentInfo[i].getBirthDate() + " " + studentInfo[i].getProgrammeOfStudy());
}
System.out.println();
}
}
}
Once I manage to print the birth years in descending order I also need to show the student name and the university modules they are doing. I know other questions have been asked how to do this but I have not been able to see one with other objects. This is a class session so please forgive any errors in my code.
一旦我设法按降序打印出生年份,我还需要显示学生姓名和他们正在做的大学模块。我知道其他问题已经被问到如何做到这一点,但我无法看到其他对象的问题。这是一堂课,所以请原谅我的代码中的任何错误。
回答by wchargin
Use a Comparator
and an ArrayList
.
使用一个Comparator
和一个ArrayList
。
In Java 8
在 Java 8 中
Use the new default and static methods on Comparator
!
使用新的默认和静态方法Comparator
!
ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos,
Comparator.comparingInt(StudentInformation::getBirthYear).reversed());
It's a brave new world! :)
这是一个勇敢的新世界!:)
Or—still better than Java 7—use lambdas!
或者——仍然比 Java 7 好——使用lambdas!
ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos, (s1, s2) ->
Integer.compare(s2.getBirthYear(), s1.getBirthYear()));
In Java 7
在 Java 7 中
Use anonymous inner classes.
使用匿名内部类。
class StudentDateComparator implements Comparator<StudentInformation> {
public int compare(StudentInformation s1, StudentInformation s2) {
return Integer.compare(s2.getBirthYear(), s1.getBirthYear());
}
}
ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos, new StudentDateComparator());
Explanation
解释
What the Comparator
does is allows anything to compare two objects of the given type (in this case, StudentInformation
). You could also make StudentInformation
implement Comparable<StudentInformation>
, but this way is probably better because there is more than one way to compare student informations (by date, as here, but also by first name, last name, number of classes enrolled, etc.).
它的Comparator
作用是允许任何东西比较给定类型的两个对象(在这种情况下,StudentInformation
)。您也可以 make StudentInformation
implement Comparable<StudentInformation>
,但这种方式可能更好,因为有不止一种方法可以比较学生信息(按日期,如此处,还可以按名字、姓氏、注册的课程数量等)。
By swapping the order of s1
and s2
in the comparator, we induce a reverse order. Another way to do this would be to negate the compare
call in the normal order, or to use a normal comparator and wrap it in Collections.reverseOrder
.
通过交换比较器中s1
和的顺序s2
,我们引入了相反的顺序。另一种方法是compare
按正常顺序取消调用,或使用正常比较器并将其包装在Collections.reverseOrder
.
You could also do this with a standard array.
您也可以使用标准数组执行此操作。
StudentInformation[] infos = new StudentInformation[10];
// fill array
Arrays.sort(infos, new StudentDateComparator());
回答by Raffaele
In the Java land, there is the Comparable<E>
interface:
在 Java 领域,有Comparable<E>
接口:
interface Comparable<E> {
public int compareTo(E other);
}
You should make your StudentInfo
implement Comparable<StudentInfo>
, implement compareTo(StudentInfo other)
according to your requirements, and either use methods from the standard library (but this will require a Comparator
instead) or some sorting algorithm suitable for your case.
您应该制作您的StudentInfo
工具Comparable<StudentInfo>
,compareTo(StudentInfo other)
根据您的要求实现,并使用标准库中的方法(但这将需要一个Comparator
替代)或一些适合您情况的排序算法。
回答by hthserhs
You can make the StudentInformation
class implement Comparable
. Then you must implement a method compareTo
. This method defines how and when two objects of the class are treated equal, smaller or bigger.
您可以使StudentInformation
类实现Comparable
. 然后你必须实现一个方法compareTo
。该方法定义了类的两个对象如何以及何时被同等、更小或更大的对待。
With this property clearly defined you can use Collections.sort
utility to sort your collection.
明确定义此属性后,您可以使用Collections.sort
实用程序对集合进行排序。