Java Comparator多字段排序不起作用

时间:2016-03-25 21:03:17

标签: java sorting

我正在尝试按姓氏降序排序,然后评分,并且一直关注此example

我只是通过姓氏排序,而不是等级排序。我没得到什么?

@Override
public int compare(Student o1, Student o2) {
    int c;
    c = o1.getLastName().compareToIgnoreCase(o2.getLastName());
    if (c == 0) {
        c = Double.compare(o1.getGrade(), o2.getGrade());
    }
    return c;
}

我并没有完全了解所提到的示例如何在c == 0方面工作,因为在排序时c永远不会等于零。这让我理解为什么成绩没有排序,但这并没有让我得到我需要的地方。

排序在这里执行:

@Override
public void execute(List<Student> list) {
    LastNameComparator cmp = new LastNameComparator(studentList);
    Collections.sort(studentList, cmp);

}

测试类是这样的:

public static void main(String[] args) {
    // TODO code application logic here

    for (int i = 0; i < 20; i++) {
        Student student = new Student(randomInteger(1,50), NameGenerator.generateName(), NameGenerator.generateName(), Math.round(Math.random()*10.0)/10.0);
        studentList.add(student);
    }
    System.out.println("--Unsorted--");

    for (Student student : studentList) {
        System.out.println(student.getStudentID());
        System.out.println(student.getFirstName());
        System.out.println(student.getLastName());
        System.out.println(student.getGrade());
    }

    Command sortLastName = new LastNameComparator(studentList);
    sortLastName.execute(studentList);

    System.out.println("--Sorted--");


    for (Student student : studentList) {
        System.out.println(student.getStudentID());
        System.out.println(student.getFirstName());
        System.out.println(student.getLastName());
        System.out.println(student.getGrade());
    }
}

名称是这样生成的。我是从here得到的:

public class NameGenerator {
    private static String[] Beginning = { "Kr", "Ca", "Ra", "Mrok", "Cru",
         "Ray", "Bre", "Zed", "Drak", "Mor", "Jag", "Mer", "Jar", "Mjol",
         "Zork", "Mad", "Cry", "Zur", "Creo", "Azak", "Azur", "Rei", "Cro",
         "Mar", "Luk" };
   private static String[] Middle = { "air", "ir", "mi", "sor", "mee", "clo",
         "red", "cra", "ark", "arc", "miri", "lori", "cres", "mur", "zer",
         "marac", "zoir", "slamar", "salmar", "urak" };
   private static String[] End = { "d", "ed", "ark", "arc", "es", "er", "der",
         "tron", "med", "ure", "zur", "cred", "mur" };

   private static Random rand = new Random();

   public static String generateName() {

      return Beginning[rand.nextInt(Beginning.length)] + 
            Middle[rand.nextInt(Middle.length)]+
            End[rand.nextInt(End.length)];

   }
} 

更新我理解这只有在名称相同的情况下才有效。这是对我不起作用的地方,因为随机名称生成器很有可能两次生成相同的名称。我正在研究如何按名称按降序排序列表,然后按等级排序,无论名称是否相同。如果名称相同,则优先考虑更高等级。

1 个答案:

答案 0 :(得分:0)

考虑学生课程如下

public class Student implements Comparable<Student> {
    String name;
    int id;
    double gpa;

    public Student(String name, int id, double gpa) {
        this.name = name;
        this.id = id;
        this.gpa = gpa;
    }

    @Override
    public int compareTo(Student o2) {
        int c;
        c = this.name.compareToIgnoreCase(o2.name);
        if (c == 0) {
            c = Double.compare(this.gpa, o2.gpa);
        }
        return c;
    }

}

然后下面的主要功能提供正确的输出

public static void main(String [] args){

ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("Milind", 1, 7.73));
studentList.add(new Student("Milind", 1, 7.70));
studentList.add(new Student("Mike", 1, 7.80));
System.out.println("--Unsorted--");

for (Student student : studentList) {
    System.out.print(student.name + ": ");
    System.out.print(student.gpa);
    System.out.println();
}
System.out.println();

Collections.sort(studentList);
System.out.println("--Sorted--");

for (Student student : studentList) {
    System.out.print(student.name + ": ");
    System.out.print(student.gpa);
    System.out.println();
}

}

输出:

--Unsorted--
Milind: 7.73
Milind: 7.7
Mike: 7.5

--Sorted--
Mike: 7.8
Milind: 7.7
Milind: 7.73
麦克首先出现,因为它是按照字母顺序排列的。并且名字Milind在接下来的2中是相同的,因此根据GPA排序。

应用相同的逻辑,您还可以根据多个字段获得输出。在这种情况下,当Firstname相同时为GPA。