• 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));
        }
    }
    
  • 相关阅读:
    Asp.Net MVC 常用开发方式之EF Code First
    整理一下Entity Framework的查询
    C#中yield return用法分析
    SQL Server表和字段说明的增加和更新
    C#中一个问号和两个问号(a ?? b)的作用
    你应该知道的25道Javascript面试题
    ASP.NET Core Razor 页面路由
    ASP.NET Core MVC – Tag Helper 组件
    ASP.NET Core 防止跨站请求伪造(XSRF/CSRF)攻击
    ASP.NET Core 使用Cookie验证身份
  • 原文地址:https://www.cnblogs.com/lhl-shubiao/p/12784490.html
Copyright © 2020-2023  润新知