zoukankan      html  css  js  c++  java
  • java 内部类实现排序 Comparator

    public class A{
    
        public xxxx fun(){
            //业务逻辑
            //xxxxxxxxxxxxxxxxxxx
            //排序
            Collections.sort(myList, new MyComparator(configValueList));
        }
        /**
        *内部类实现排序
        *configValueList 排序规则
        *根据DtoList中的某一个字段,按照configValueList配置的规则来排序
        *如configValueList ["a","b","c"]
        *myList    myList.get[0].getVal() = b,myList.get[1].getVal() = a,myList.get[2].getVal() = c
        *那么排序后 myList.get[0].getVal() = a,myList.get[1].getVal() = b,myList.get[2].getVal() = c
        */
        class MyComparator implements Comparator<Dto> {
            private List<String> configValueList;
    
            public MyComparator(List<String> configValueList) {
                this.configValueList = configValueList;
            }
    
            @Override
            public int compare(Dto dto1, Dto dto2) {
                if(CollectionUtils.isEmpty(configValueList) || dto1 == null || dto2 == null){
                    return 0;
                }
                String val1 = dto1.getVal();
                String val2 = dto2.getVal();
                if(StringUtils.isBlank(val1) || StringUtils.isBlank(val2)){
                    return 0;
                }
                int sort1 = configValueList.indexOf(val1);
                int sort2 = configValueList.indexOf(val2);
                if(-1 == sort1 || -1 == sort2){
                    return 0;
                }
                return sort1 - sort2;
            }
        }
    }
  • 相关阅读:
    c++ this *this
    名称空间
    c++ 静态持续变量
    c++ 数组
    c++ 头文件
    实例化和具体化详解
    在linux下安装eclipse以及运行c++程序的安装步骤
    在centos (linux) 搭建 eclipse c++开发分环境
    Linux上使用Qt Creator进行C/C++开发
    使用Qt Creator 2.60编写C/C++程序
  • 原文地址:https://www.cnblogs.com/liuwt365/p/7204285.html
Copyright © 2011-2022 走看看