在int属性上按降序对自定义对象数组进行排序

Pri*_*ist 6 java arrays

我想按出生年份的降序排序我的数组.我的数组有另外两个String类型的元素.因此,作为一个例子,出生在最早年的人,如1939年,将处于最顶层,然后如此.

这是我的代码:

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();
}

 }

}
Run Code Online (Sandbox Code Playgroud)

一旦我设法按降序打印出生年份,我还需要显示他们正在做的学生姓名和大学模块.我知道其他问题已经被问到如何做到这一点,但我还没有看到一个与其他对象.这是一个类会话,请原谅我的代码中的任何错误.

wch*_*gin 20

使用a ComparatorArrayList.

在Java 8中

使用新的默认和静态方法Comparator!

ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos, 
    Comparator.comparingInt(StudentInformation::getBirthYear).reversed());
Run Code Online (Sandbox Code Playgroud)

这是一个勇敢的新世界!:)

或者仍然比Java 7使用lambdas更好!

ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos, (s1, s2) ->
    Integer.compare(s2.getBirthYear(), s1.getBirthYear()));
Run Code Online (Sandbox Code Playgroud)

在Java 7中

使用匿名内部类.

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());
Run Code Online (Sandbox Code Playgroud)

说明

它的Comparator作用是允许任何东西比较给定类型的两个对象(在这种情况下StudentInformation).你也可以制作StudentInformation工具Comparable<StudentInformation>,但这种方式可能更好,因为有多种方法来比较学生的信息(按日期,在这里,但也可以通过名字,姓氏,登记的班级数等).

通过交换比较器中的s1和的顺序s2,我们引起相反的顺序.另一种方法是compare按正常顺序否定调用,或使用普通比较器并将其包装Collections.reverseOrder.


您也可以使用标准数组执行此操作.

StudentInformation[] infos = new StudentInformation[10];
// fill array
Arrays.sort(infos, new StudentDateComparator());
Run Code Online (Sandbox Code Playgroud)