Sort an array of custom objects in descending order on an int property
arrays, java
Solution
Use a `Comparator` and an `ArrayList`.
In Java 8
Use the new default and static methods on `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!
ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos, (s1, s2) ->
Integer.compare(s2.getBirthYear(), s1.getBirthYear()));
In 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.).
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`.
You could also do this with a standard array.
StudentInformation[] infos = new StudentInformation[10];
// fill array
Arrays.sort(infos, new StudentDateComparator());
Problem
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. 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.