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;
        }
    }
}

 

相关文章:

  • 2021-10-30
  • 2022-02-13
  • 2022-12-23
  • 2021-04-17
  • 2022-12-23
  • 2019-05-28
  • 2022-12-23
  • 2021-08-21
猜你喜欢
  • 2021-11-14
  • 2021-10-28
  • 2022-12-23
  • 2021-11-06
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
相关资源
相似解决方案