排序

1、自定义类自然排序

package demo;

import java.util.Arrays;

/**
 * @description: demo20
 * @author: liuyang
 * @create: 2021-08-30 21:35
 */
public class Demo20 {
    public static void main(String[] args) {
        Person p1 = new Person("aa", 300);
        Person p2 = new Person("bb", 30);
        Person p3 = new Person("cc", 310);
        Person p4 = new Person("cc", 2);
        Person[] arr = new Person[] {p1, p2, p3, p4};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

class Person implements Comparable<Person> {
    private String name;
    private double price;

    public Person() {
    }

    public Person(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

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

    @Override
    public int compareTo(Person o) {
        return Double.compare(this.price, o.price);
    }
}

2、定制排序

package demo;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @description: demo21
 * @author: liuyang
 * @create: 2021-08-30 22:31
 */
public class Demo21 {
    public static void main(String[] args) {
        String[] strArr = new String[] {"DDD","AAA","CCC","BBB"};
        Arrays.sort(strArr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                // 降序排序
                return o2.compareTo(o1);
            }
        });
        System.out.println(Arrays.asList(strArr));

        Person1 p1 = new Person1("aa", 300);
        Person1 p2 = new Person1("bb", 30);
        Person1 p3 = new Person1("cc", 310);
        Person1 p4 = new Person1("cc", 2);
        Person1[] personArr = new Person1[] {p1, p2, p3, p4};
        Arrays.sort(personArr, new Comparator<Person1>() {
            @Override
            public int compare(Person1 o1, Person1 o2) {
                // 按照价格降序排序
                return Double.compare(o2.getPrice(), o1.getPrice());
            }
        });
        System.out.println(Arrays.toString(personArr));
    }
}

class Person1 {
    private String name;
    private double price;

    public Person1() {
    }

    public Person1(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

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

 

posted @ 2021-08-30 21:58  牛牛的自留地  阅读(14)  评论(0编辑  收藏  举报