I understand it

Comparable focuses more on the inherent comparison methods within collections, with packaging types such as Integer,Double, and Long all having their own comparison methods; Comparable focuses more on artificial comparisons that do not change the original class, whereas Comparable requires that the comparison be written when the class is constructed

The code to understand

Suppose we focus only on the Student score and name, there is the Student class as follows:

class Student implements Comparable<Student>{
    private Double score;
    private String name;

    public Student(Double score, String name) {
        this.score = score;
        this.name = name;
    }

    public Double getScore(a) {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Student o) {
        return this.score.compareTo(o.score);
    }

    @Override
    public String toString(a) {
        return "score=" + score + ", name='" + name +"'"; }}Copy the code

Order Student’s container in ascending order:

public class CompareTest {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(92.0."Zhang"));
        list.add(new Student(95.0."Bill"));
        list.add(new Student(89.0."Fifty"));
        list.add(new Student(77.0."Daisy"));
        list.add(new Student(99.0."Sloppy"));
        list.add(new Student(100.0."Sloppy."));
        Collections.sort(list);
        for(Student s :list) { System.out.println(s); }}}Copy the code

Results:

score=77.0, name='Daisy'
score=89.0, name='Cathy'
score=92.0, name='Joe'
score=95.0, name='bill'
score=99.0, name='Careless'
score=100.0, name='Careless'
Copy the code

If we want to sort by name without modifying Student’s class structure, we need to write another class (we could also use lambda or anonymous classes) :

class CompareByName implements Comparator<Student>{
    public CompareByName(a) {}@Override
    public int compare(Student o1, Student o2) {
    	The String class also implements the Comparable interface
        returno1.getName().compareTo(o2.getName()); }}Copy the code

Modify the test Demo as follows:

public class CompareTest {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(92.0."Zhang"));
        list.add(new Student(95.0."Bill"));
        list.add(new Student(89.0."Fifty"));
        list.add(new Student(77.0."Daisy"));
        list.add(new Student(99.0."Sloppy"));
        list.add(new Student(100.0."Sloppy."));

        Collections.sort(list,new CompareByName());
        for(Student s :list) { System.out.println(s); }}}Copy the code

The result is a comparison by name (Chinese by Unicode, English by ASCII).

score=99.0, name='Careless'
score=100.0, name='Careless'
score=92.0, name='Joe'
score=95.0, name='bill'
score=89.0, name='Cathy'
score=77.0, name='Daisy'
Copy the code

Thank you for your patience