比较器没有正确比较值

时间:2014-03-15 16:56:34

标签: java list comparator

我有这个比较器,比较每个标记的标记,并从最高到最低排序。唯一的问题是主题列表绑定在学生对象中。所以每个学生都有很多科目。总标记为double型。比较器不能正常工作。

public class RankComparator implements Comparator<Subject> {

        @Override
        public int compare(Subject s1, Subject s2) {

            if (s1.getoveralMark() < s2.getoveralMark())
                return -1;
            if (s1.getoveralMark() > s2.getoveralMark())
                return 1;

            return 0;

        }

    }

更新:我添加了以下代码,但仍然没有按最高的overalMark排序主题。

 for (Student student : studentList) {
                // if (subj.subjectName.contains("English")) {

                for (Subject subj : student.subject) {
                    Collections.sort(student.subject, new RankComparator());
                    System.out.println(subj.overalMark);

                }


Collections.sort(studentList, compareByAllSubjects());

        for (Student student : studentList) {
            System.out.println(student.firstName);
            for (Subject subk : student.subject) {

                System.out.println(subk.subjectName);
                System.out.println(subk.overalMark);

            }
        }

        // }
    }

    private static Comparator<Student> compareBySubject(final int index) {
        return new Comparator<Student>() {
            @Override
            public int compare(Student student0, Student student1) {
                Subject subject0 = student0.subject.get(index);
                Subject subject1 = student1.subject.get(index);
                if (subject0.getSubjectName() == subject1.getSubjectName()) {
                    double mark0 = subject0.getoveralMark();
                    double mark1 = subject1.getoveralMark();
                    return Double.compare(mark0, mark1);
                }
                return 0;
            }
        };
    }

    private static double computeMaxMark(List<Subject> subjects) {
        double max = -Double.MAX_VALUE;
        for (Subject subject : subjects) {
            max = Math.max(max, subject.getoveralMark());
        }
        return max;
    }

    private static Comparator<Student> compareByAllSubjects() {
        return new Comparator<Student>() {
            @Override
            public int compare(Student student0, Student student1) {
                double mark0 = computeMaxMark(student0.getSubject());
                double mark1 = computeMaxMark(student1.getSubject());
                return Double.compare(mark0, mark1);
            }
        };
    }

1 个答案:

答案 0 :(得分:0)

因此,根据评论,您希望根据学生在一个科目中取得的成绩对学生进行排序。在这种情况下,您不需要Comparator<Subject>而是Comparator<Student> - 即比较学生的比较器。

然而,这个比较器必须知道&#34;根据哪个学科应该比较学生。在这种情况下,可能有必要知道所有学生是否具有相同的科目,或者每个学生的科目组是否可能不同。如果它们可能不同,则必须考虑排序应如何处理具有学生应根据其分类的学生。

但是,在此示例中显示了创建比较器的一般方法,该比较器基于他们在特定主题中实现的标记对学生进行排序:

(编辑:添加了另一个排序标准,这似乎是一个猜谜游戏....)

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

public class StudentSorting
{
    public static void main(String[] args)
    {
        Random random = new Random(0);
        List<Student> students = new ArrayList<Student>();
        for (int i=0; i<3; i++)
        {
            Student student = new Student("Student"+i);
            student.addSubject(new Subject("A", random.nextInt(6)));
            student.addSubject(new Subject("B", random.nextInt(6)));
            student.addSubject(new Subject("C", random.nextInt(6)));
            students.add(student);
        }

        for (int s=0; s<3; s++)
        {
            System.out.println("Students compared by subject "+s);
            Collections.sort(students, compareBySubject(s));
            for (Student student : students)
            {
                System.out.println("    "+student);
            }
        }

        System.out.println("Students compared all subjects");
        Collections.sort(students, compareByAllSubjects());
        for (Student student : students)
        {
            System.out.println("    "+student);
        }

    }

    /**
     * Creates a comparator that compares students by the overall mark
     * that they achieved in the subject with the given index
     *
     * @param index The index of the subject
     * @return The comparator
     */
    private static Comparator<Student> compareBySubject(final int index)
    {
        return new Comparator<Student>()
        {
            @Override
            public int compare(Student student0, Student student1)
            {
                Subject subject0 = student0.getSubject(index);
                Subject subject1 = student1.getSubject(index);
                double mark0 = subject0.getOverallMark();
                double mark1 = subject1.getOverallMark();
                return Double.compare(mark0, mark1);
            }
        };
    }

    /**
     * Creates a comparator that compares students by the overall mark
     * that they achieved in any subject
     *
     * @return The comparator
     */
    private static Comparator<Student> compareByAllSubjects()
    {
        return new Comparator<Student>()
        {
            @Override
            public int compare(Student student0, Student student1)
            {
                double mark0 = computeMaxMark(student0.getSubjects());
                double mark1 = computeMaxMark(student1.getSubjects());
                return Double.compare(mark0, mark1);
            }
        };
    }

    /**
     * Computes the maximum overall mark of the given subjects
     *
     * @param subjects The subjects
     * @return THe maximum mark
     */
    private static double computeMaxMark(List<Subject> subjects)
    {
        double max = -Double.MAX_VALUE;
        for (Subject subject : subjects)
        {
            max = Math.max(max, subject.getOverallMark());
        }
        return max;
    }

}


class Subject
{
    private final String name;
    private final double mark;

    Subject(String name, double mark)
    {
        this.name = name;
        this.mark = mark;
    }

    double getOverallMark()
    {
        return mark;
    }

    @Override
    public String toString()
    {
        return name+"("+mark+")";
    }
}

class Student
{
    private final String name;
    private final List<Subject> subjects;

    Student(String name)
    {
        this.name = name;
        this.subjects = new ArrayList<Subject>();
    }

    void addSubject(Subject subject)
    {
        subjects.add(subject);
    }

    Subject getSubject(int index)
    {
        return subjects.get(index);
    }

    List<Subject> getSubjects()
    {
        return subjects;
    }

    @Override
    public String toString()
    {
        return name+" with "+subjects;
    }
}