zoukankan      html  css  js  c++  java
  • comparatorChain实现对Bean多字段排序

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.commons.beanutils.BeanComparator;
    import org.apache.commons.collections.comparators.ComparatorChain;
    
    import com.google.gson.Gson;
    
    public class Main {
        
        
        public static class Cats{
            private Integer years;
            private String  name;
            private String  entryDate;
            public Integer getYears() {
                return years;
            }
            public void setYears(Integer years) {
                this.years = years;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public String getEntryDate() {
                return entryDate;
            }
            public void setEntryDate(String entryDate) {
                this.entryDate = entryDate;
            }
        }
    
        
        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
            List<Cats> cats = new ArrayList<>();
            Cats cat = new Cats();
            cat.setEntryDate(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss"));
            cat.setName("smith");
            cat.setYears(21);
            
            Cats cat1 = new Cats();
            cat1.setEntryDate(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss"));
            cat1.setName("KAMIER");
            cat1.setYears(36);
            
            Cats cat2 = new Cats();
            cat2.setEntryDate(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss"));
            cat2.setName("STIWEN");
            cat2.setYears(18);
            
            Cats cat3 = new Cats();
            cat3.setEntryDate(DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss"));
            cat3.setName("AKAI");
            cat3.setYears(88);
            cats.add(cat3);cats.add(cat2);cats.add(cat1);cats.add(cat);
            
            ComparatorChain comparatorChain = new ComparatorChain();
            comparatorChain.addComparator(new BeanComparator("entryDate"));
            //comparatorChain.addComparator(new BeanComparator("years"));
    //      Arrays.sort(cats.toArray(), comparatorChain);
            Collections.sort(cats, comparatorChain);
            System.out.println("多字段排序:");
            System.out.println(new Gson().toJson(cats));
        }
    }
    
  • 相关阅读:
    DBCC Page查看表和索引数据
    sp_configure
    学习
    OpenRowSet, OpenDataSource
    sp output
    SQL Server中使用CLR调用.NET方法
    SQL Server 2005五个有用的动态管理对象
    SQL显示执行语句信息
    接规则,每三个一组编一个号
    C# winform 与 flash as 的交互通讯
  • 原文地址:https://www.cnblogs.com/lhl-shubiao/p/12784490.html
Copyright © 2011-2022 走看看