How to use qsort to sort classes in JAVA?

Table of contents

in conclusion: 

Parse: 


in conclusion: 

import java.util.Arrays;

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

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Person o) {
        return this.age - o.age;
    }
}

public class Main{

    public static void main(String args[]){
        Person[] arr = new Person[3];
        arr[0] = new Person("zhang", 10);
        arr[1] = new Person("wang", 13);
        arr[2] = new Person("li", 11);

        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

 

Parse: 

We know that when we define an integer array and then want to sort it, we can use the Arrays.toString() method to sort the array, and the system will sort it in ascending order by default.

    public static void main(String args[]){
        int[] arr = {2,6,3,0,5,2};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }

 

What should we do when we need to sort classes?

First we define a class:

class Person{
    public String name;
    public int age;

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 Now that we have this class, we can sort it. At this point, we can try to sort the class directly:

    public static void main(String args[]){
        Person[] arr = new Person[3];
        arr[0] = new Person("zhang", 10);
        arr[1] = new Person("wang", 13);
        arr[2] = new Person("li", 11);

        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }

 Although an error was reported, we can find this piece of code from the error message:

 From this we can clearly see that it casts our generation sorting class into Comparable  and our class cannot be cast into Comparable at all, which causes an error.

Now that we know where the error occurred, we can implement the  Arrays.toString() method to sort the classes after we correct it.

At this point, we first modify our class to implement  the Comparable interface:

Then rewrite the compareTo() method in the class. (If we use age to sort the return value of the compareTo() method: greater than returns a number greater than 0; less than returns a number less than zero; equal returns 0 )

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

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Person o) {
        return this.age - o.age;
    }
}

After running, you can see that the results are sorted in ascending order by age. 

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/132429661