根据教师ID对学生列表进行排序

时间:2016-05-26 06:53:34

标签: java sorting collections comparator

我有一个与他们联系的老师ID的学生名单。下面是学生豆。

public class Student {
    private Integer Id;
    private String Name;
    private Integer teacherId;

public Student(Integer empId, String empName, Integer teacherId) {
    super();
    this.empId = empId;
    this.empName = empName;
    this.teacherId = teacherId;
}
   // setters getters
}

现在在我的主要课程中,我有学生名单,其中包含以下数据。

public static void main(String[] args) {
     int teacherId = 6;
    List<Student> list = new ArrayList<Student>();

    list.add(new Student(1002, "Kausik",4));
    list.add(new Student(1004, "Tridibendu",6));
    list.add(new Student(1007, "Ram",8));
    list.add(new Student(1003, "Neeraj",6));
    list.add(new Student(1009, "Manish",8));
    list.add(new Student(1001, "Shyam",1));

    // Sorting collection in ascending order based on teacher id
    Collections.sort(list, new StudentComparator());

}

下面是我的学生比较班。任何人都可以帮我解决我在比较方法中写的内容,这样如果我将2作为教师ID传递,则列表应按照与教师ID 2联系的所有学生应该在顶部并且其余部分必须在底部的方式进行排序。

或者我还有其他方法可以在java中执行此操作吗?

public class StudentComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {        
        return o1.getTeacherId().compareTo(o2.getTeacherId());
    }
}

以下是我得到的输出。

Id: 1001, Name: Shyam, TeacherId: 1
Id: 1002, Name: Kausik, TeacherId: 4
Id: 1004, Name: Tridibendu, TeacherId: 6
Id: 1003, Name: Neeraj, TeacherId: 6
Id: 1007, Name: Ram, TeacherId: 8
Id: 1009, Name: Manish, TeacherId: 8

但我希望输出基于传递的教师ID作为参数。如果我将6作为教师ID传递,那么我的输出应该在下面:

Id: 1004, Name: Tridibendu, TeacherId: 6
 Id: 1003, Name: Neeraj, TeacherId: 6
 Id: 1001, Name: Shyam, TeacherId: 1
 Id: 1002, Name: Kausik, TeacherId: 4
 Id: 1007, Name: Ram, TeacherId: 8
 Id: 1009, Name: Manish, TeacherId: 8

所以老师有id,因为6应该在列表之上。

2 个答案:

答案 0 :(得分:2)

您可以做的是Student实施Comparable<Student>。为此,你可以这样做:

public class Student implements Comparable<Student> {

...

    @Override
    public int compareTo(Student s) {
        return this.teacherId.compareTo(s.teacherId);
    }
}

与您的其他事项相关,您可以实施另一个比较器,它允许您根据给定的教师ID将列表拆分为两部分。

public class StudentComparator implements Comparator<Student> {
    private Integer id;

    public StudentComparator(Integer id) {
        this.id = id;
    }

    @Override
    public int compare(Student o1, Student o2) {
        int id1 = o1.getTeacherId(); // Let the unboxing do its job
        int id2 = o2.getTeacherId();
        if (id1 == id2) return 0;
        if (id1 == this.id) return 1;
        if (id2 == this.id) return -1;
        return 0;
    }
}

因此,只要您想根据一个教师ID对列表进行分区,就可以致电Collections.sort(studentList, new StudentComparator(teacherId))

答案 1 :(得分:1)

您可以强制它为匹配的教师返回否定值,然后通常对其余部分进行排序:

public class StudentComparator implements Comparator<Student> {
    private int contactedTeacher;
    public StudentComparator(int contactedTeacherId) {
        this.contactedTeacher = contactedTeacherId;
    }
    @Override
    public int compare(Student o1, Student o2) {
        if(this.contactedTeacher == o1.teacherId) {
            return -1; //Force negative
        } else if(this.contactedTeacher == o2.teacherId) {
            return 1; //Force positive
        }

        //If neither of the compared students contacted the teacher, compare normally by teacher ID
        return o1.getTeacherId().compareTo(o2.getTeacherId());
    }
}

然后您的排序呼叫变为:

Collections.sort(list, new StudentComparator(teacherId));