在Java中使用比较器比较对象属性

在Java中使用比较器比较对象属性,java,comparable,Java,Comparable,我正在做DS/A作业,我一辈子都不知道如何使用Comparable/Comparator接口将对象的特定属性标记为比较和排序的键。我们正在使用一个提供给我们的Mergesort函数,我相信除了我收到的这个错误之外,我已经在程序中完成了大部分其他工作: incompatible types: Student[] cannot be converted to Comparable[] 我非常希望能够深入了解这个问题,因为我对Java和OOP还是相当陌生的。以下是我得到的: Test

我正在做DS/A作业,我一辈子都不知道如何使用Comparable/Comparator接口将对象的特定属性标记为比较和排序的键。我们正在使用一个提供给我们的Mergesort函数,我相信除了我收到的这个错误之外,我已经在程序中完成了大部分其他工作:

        incompatible types: Student[] cannot be converted to Comparable[]
我非常希望能够深入了解这个问题,因为我对Java和OOP还是相当陌生的。以下是我得到的:

TestMergeSort.java:

public class TestMergeSort {

public static void main(String[] args) {
    Student[] students = initializeStudentsArray();

    System.out.println("Displaying students array before sorting...");
    display(students);

    System.out.println("Being sorting...");
    /*
     * The Student array will be sorted by studentId
     * which is declared as a String
     */
    Merge.sort(students); // TODO: Fix the Student class to eliminate this error
    System.out.println("End sorting...");

    System.out.println("Displaying students array after sorting...");
    display(students);
}

private static Student[] initializeStudentsArray() {

    Student[] students = new Student[5];
    students[0] = new Student("Joe", "Jones", "1001");
    students[1] = new Student("Adam", "Ant", "950");
    students[2] = new Student("Bill", "Barnes", "735");
    students[3] = new Student("Mark", "Roth", "1102");
    students[4] = new Student("Jerome", "Howard", "1150");
    return students;

}

private static void display(Student[] students) {
    for (Student std : students) {
        System.out.println("Students [firstName=" + std.firstName + ", lastName=" + std.lastName + ", studentId=" + std.studentId + "]");
    }
}
}

和Student.java:

    public class Student implements Comparator<Student> {

    public String firstName;
    public String lastName;
    public String studentId;

    public Student(String first, String last, String Id) {

        this.firstName = first;
        this.lastName = last;
        this.studentId = Id;
    }

    @Override
    public int compare(Student stud1, Student stud2) {

        String student1 = stud1.studentId;
        String student2 = stud2.studentId;

        return student1.compareTo(student2);
    }
}
公共类学生实现比较器{
公共字符串名;
公共字符串lastName;
公共字符串学生ID;
公立学生(字符串第一,字符串最后,字符串Id){
this.firstName=first;
this.lastName=last;
this.studentId=Id;
}
@凌驾
公共整数比较(学生学习1、学生学习2){
字符串student1=stud1.studentId;
字符串student2=stud2.studentId;
返回学生1.与(学生2)进行比较;
}
}
我可能做错了什么,所以请给我一点提示。非常感谢您抽出时间

Merge.sort(students);
我不知道合并类是什么。我只使用默认的Collections.sort(…)

我不知道合并类是什么。我只使用默认的Collections.sort(…)


您的学生班级应实施可比较和非比较。您的学生班级应实施可比较和非比较。
public class Student implements Comparator<Student> {
/*
**  Use the Collections API to sort a List for you.
**
**  When your class has a "natural" sort order you can implement
**  the Comparable interface.
**
**  You can use an alternate sort order when you implement
**  a Comparator for your class.
*/
import java.util.*;

public class Person implements Comparable<Person>
{
    String name;
    int age;

    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }

    public String toString()
    {
        return name + " : " + age;
    }

    /*
    **  Implement the natural order for this class
    */
    public int compareTo(Person p)
    {
        return getName().compareTo(p.getName());
    }

    static class AgeComparator implements Comparator<Person>
    {
        public int compare(Person p1, Person p2)
        {
            return p1.getAge() - p2.getAge();
        }
    }

    public static void main(String[] args)
    {
        List<Person> people = new ArrayList<Person>();
        people.add( new Person("Homer", 38) );
        people.add( new Person("Marge", 35) );
        people.add( new Person("Bart", 15) );
        people.add( new Person("Lisa", 13) );

        // Sort by natural order

        Collections.sort(people);
        System.out.println("Sort by Natural order");
        System.out.println("\t" + people);

        // Sort by reverse natural order

        Collections.sort(people, Collections.reverseOrder());
        System.out.println("Sort by reverse natural order");
        System.out.println("\t" + people);

        //  Use a Comparator to sort by age

        Collections.sort(people, new Person.AgeComparator());
        System.out.println("Sort using Age Comparator");
        System.out.println("\t" + people);

        //  Use a Comparator to sort by descending age

        Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator()));
        System.out.println("Sort using Reverse Age Comparator");
        System.out.println("\t" + people);

        //  Use a Comparator with lambda expression to sort by age

//      Collections.sort(people, (o1, o2) -> o1.getAge() - o2.getAge());
//      Collections.sort(people, Comparator.comparingInt(p -> p.getAge()));
        Collections.sort(people, Comparator.comparingInt(Person::getAge));
        System.out.println("Sort using Lambda Age Comparator");
        System.out.println("\t" + people);
    }
}