sort 根据实体类中指定的列名对List中的对象进行排序

在list中经常需要对其中的对象进行排序,有时候排序的需求不一样,根据不同的字段进行排序。

这就使用到了比较器 comparator


不废话,上代码:

下面是3个代码文件。

第一个是实体类,书籍:

package userSort;


public class Book {
    
    private Long id;//书本编号
    private String name;//书本名称
    private double price;//书本价格
    private String author;//作者
    private Integer weight;//权重
    
    public Book(Long Id,String Name,double Price,String Author,Integer Weight) {
        this.id=Id;
        this.name=Name;
        this.price=Price;
        this.author=Author;
        this.weight=Weight;
}
    
    
    
    public Long getId() {
        return id;
    }
    
    
    public void setId(Long id) {
        this.id = id;
    }
    
    
    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;
    }
    
    
    public String getAuthor() {
        return author;
    }
    
    
    public void setAuthor(String author) {
        this.author = author;
    }



    
    public Integer getWeight() {
        return weight;
    }



    
    public void setWeight(Integer weight) {
        this.weight = weight;
    }
    
}


第二个代码文件就是本次的重点了,比较器:实现对任何指定的列名进行排序

package userSort;

import java.lang.reflect.Method;
import java.util.Comparator;

/**
 * 此文件是根据输入的参数进行排序 
 * eg: 输入参数为  (String propertyName ,boolean isASC) 返回的结果为 list, 不过是根据propertyName升序排列后的结果 
 * 根据Student类中的propertyName字段进行排序。
 * T表示是Student类,propertyName是类中的一个字段名称 
 * 
 * @author YinLinFei
 * @param 
 */

/**
 * 字段名的首字母需要大写
 * @author YinLinFei
 *
 */
public class sortList implements Comparator {

    private String  propertyName;
    private boolean isAsc;

    public sortList(String propertyname, boolean isasc) {
        this.propertyName = propertyname;
        this.isAsc = isasc;
    }

    /**
     * 需要的是:根据类中的字段对对象进行排序
     * 
     * @return
     */

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public int compare(T b1, T b2) {

        Class clz = b1.getClass();
        Method mth = getPropertyMethod(clz, propertyName);

        try {
            Object o1 = mth.invoke(b1);
            Object o2 = mth.invoke(b2);

            if (o1 == null || o2 == null) return 0;
            Comparable value1 = (Comparable) o1;
            Comparable value2 = (Comparable) o2;

            if (isAsc) {
                return value1.compareTo(value2);
            } else {
                return value2.compareTo(value1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;

        //原先的固定的根据某一个固定的字段进行排序
        // if (b1 == null || b2 == null) {
        // return 0;
        // }
        // if (isAsc) {
        // return b1.getWeight().compareTo(b2.getWeight());
        // } else {
        // return b2.getWeight().compareTo(b1.getWeight());
        // }
    }

    // 获取类名
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Method getPropertyMethod(Class clz, String propertyName) {
        Method mth = null;
        try {
            mth = clz.getMethod("get" + propertyName);
        } catch (Exception e) {
            System.out.println("获取类名发生错误!");
        }
        return mth;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public boolean isAsc() {
        return isAsc;
    }

    public void setAsc(boolean isAsc) {
        this.isAsc = isAsc;
    }

}



最后一个是测试文件:

package userSort;

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

public class Main {

    public static List getBookList() {
        List books = new ArrayList();
        Book book1 = new Book(1L, "first", 10.00, "zhangsan", 19);
        Book book2 = new Book(2L, "first", 9.00, "zhangsan", 24);
        Book book3 = new Book(3L, "first", 8.00, "zhangsan", 29);
        Book book4 = new Book(4L, "first", 7.00, "zhangsan", 13);
        Book book5 = new Book(5L, "first", 6.00, "zhangsan", 14);

        books.add(book1);
        books.add(book2);
        books.add(book3);
        books.add(book4);
        books.add(book5);

        return books;
    }

    /**
     * 打印函数
     * 
     * @param lisbk
     */
    public static void printf(List lisbk) {
        if (lisbk.isEmpty() || lisbk == null) {
            System.out.println("没有数据");
            return;
        }
        for (Book book : lisbk) {
            System.out.println("Id: " + book.getId() + "   price: " + book.getPrice() + "  weight:" + book.getWeight());
        }
       System.out.println();
        return;
    }

    public static void main(String[] args) {
        List bks = getBookList();
        System.out.println("原先的顺序:");
        printf(bks);
        
        System.out.println("根据价格排序:");
        Collections.sort(bks, new sortList("Price",true));
        printf(bks);
        
        System.out.println("根据Id排序:");
        Collections.sort(bks, new sortList("Id",true));
        printf(bks);
        
        System.out.println("根据weight排序:");
        Collections.sort(bks, new sortList("Weight",true));
        printf(bks);
        
    }

}




打印结果:

Id: 1   price: 10.0  weight:19
Id: 2   price: 9.0  weight:24
Id: 3   price: 8.0  weight:29
Id: 4   price: 7.0  weight:13
Id: 5   price: 6.0  weight:14

根据价格排序:
Id: 5   price: 6.0  weight:14
Id: 4   price: 7.0  weight:13
Id: 3   price: 8.0  weight:29
Id: 2   price: 9.0  weight:24
Id: 1   price: 10.0  weight:19

根据Id排序:
Id: 1   price: 10.0  weight:19
Id: 2   price: 9.0  weight:24
Id: 3   price: 8.0  weight:29
Id: 4   price: 7.0  weight:13
Id: 5   price: 6.0  weight:14

根据weight排序:
Id: 4   price: 7.0  weight:13
Id: 5   price: 6.0  weight:14
Id: 1   price: 10.0  weight:19
Id: 2   price: 9.0  weight:24
Id: 3   price: 8.0  weight:29



你可能感兴趣的:(java)