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.
To sort an array in Java in descending order, you have to use the reverseOrder() method from the Collections class. The reverseOrder() method does not parse the array. Instead, it will merely reverse the natural ordering of the array.
Example 1: Sort Array by Property NameThe sort() method sorts its elements according to the values returned by a custom sort function ( compareName in this case). Here, The property names are changed to uppercase using the toUpperCase() method. If comparing two names results in 1, then their order is changed.
Approach: An ArrayList can be Sorted by using the sort() method of the Collections Class in Java. This sort() method takes the collection to be sorted and Collections. reverseOrder() as the parameter and returns a Collection sorted in the Descending Order.
Use a Comparator
and an ArrayList
.
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()));
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());
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());
In the Java land, there is the Comparable<E>
interface:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With