自定义排序规则 实现 Comparable<> Comparator<>接口两种方式

自定义排序规则     实现Comparable接口,并重写CompareTo()方法,在测试类中使用Collections.sort(peopleList);

package edu.chinasoft.definepaixuguize;

/**
 * Encoding:utf-8
 * Author: bailanglang
 * Date:  2020-10-22 20:28
 * Version:1.0
 * Description:TODO
 */

public class People implements  Comparable<People>{
    private int id;
    private String name;
    private int  age;

    public People() {
    }

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

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "People{" +
                "人的id=" + id +
                ", 人的姓名='" + name + '\'' +
                ", 人的年龄=" + age +
                '}';
    }

    @Override
    public int compareTo(People o) {
        return this.age>o.age?1: ((this.age == o.age)?0:-1);   //返回-1 是逆序
    }
}
package edu.chinasoft.definepaixuguize;

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

/**
 * Encoding:utf-8
 * Author: bailanglang
 * Date:  2020-10-22 20:36
 * Version:1.0
 * Description:TODO
 */

public class testPeopleComparable {
    /**
     * @author bailanglang
     * Description 主方法
     */
    public static void main(String[] args) {
        List<People> peopleList = new ArrayList<>();
        peopleList.add(new People(001,"白浪",22));
        peopleList.add(new People(002,"孙星星",25));
        peopleList.add(new People(003,"高玮",24));

        System.out.println("排序前:"+peopleList);
        Collections.sort(peopleList);     //对集合排序(需对Javabean定义排序规则),使用Collections.
        System.out.println("排序后:"+peopleList);


    }
}

 

 

自定义排序规则   正常pojo类Book,在另外类中实现Comparator<pojo>接口,并重写比较方法compare(Book o1, Book o2)

在测试类中调用Collections.sort(bookList,new BookPrice());

package edu.chinasoft.definepaixuguize;
public class Book {
        private int bookId;
        private String bookName;
        private double bookPrice;
        public Book() {
        }

    public Book(int bookId, String bookName, double bookPrice) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookPrice = bookPrice;
    }

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public double getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(double bookPrice) {
        this.bookPrice = bookPrice;
    }

    @Override
    public String toString() {
        return
                "书号:" + bookId +
                ", 书名:'" + bookName + '\'' +
                ", 书价:" + bookPrice ;
    }
}

  

 

package edu.chinasoft.definepaixuguize;
import java.util.Comparator;
public class BookPrice implements Comparator<Book>{
    @Override
    public int compare(Book o1, Book o2) {
        return o1.getBookPrice()>o2.getBookPrice()?1 :(o1.getBookPrice() == o2.getBookPrice()?0:-1);    //加负号,表示逆序
    }
}

  

 

 

 

package edu.chinasoft.definepaixuguize;

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

/**
 * Encoding:utf-8
 * Author: bailanglang
 * Date:  2020-10-22 20:54
 * Version:1.0
 * Description:TODO
 */

public class testBookPriceComparator {
    /**
     * @author bailanglang
     * Description 主方法 
     */
    public static void main(String[] args) {


        List<Book> bookList = new ArrayList<Book>();
        bookList.add(new Book(1,"计算机网络",15.5));
        bookList.add(new Book(3,"数据结构",35.9));
        bookList.add(new Book(2,"操作系统",29.9));
        //排序
        Collections.sort(bookList,new BookPrice());
        for(int i=0;i< bookList.size();i++){
            System.out.println(bookList.get(i));
        }
    }
}

  

 

posted @ 2020-10-22 23:19  BAILANGL  阅读(59)  评论(0编辑  收藏  举报