• 实现场景:集合对象按照自定义规则排序【代码】


    原理:

      利用Collections.sort的自定义排序功能 + 自己写一个确定对象字段下标的方法,判断下标的大小确定自定义顺序。

     1 public class TestCollection {
     2     String yearOrder = "全网,广东,广西,云南,贵州,海南,广州,深圳";
     3 
     4     @Autowired
     5     private DlgyIndexYearMapper dlgyIndexYearMapper;
     6     
     7     public int getIndex(String order, String value) {
     8         if (order != null && value != null) {
     9             return order.indexOf(value);
    10         }else {
    11             return 100;
    12         }
    13     }
    14     
    15     public List<DlgyIndexYear> sort(List<DlgyIndexYear> list) {
    16         if (list != null) {
    17             Collections.sort(list, new Comparator<DlgyIndexYear>() {
    18                 public int compare(DlgyIndexYear o1, DlgyIndexYear o2) {
    19                     int index_1 = new TestCollection().getIndex(yearOrder, o1.getType1());
    20                     int index_2 = new TestCollection().getIndex(yearOrder, o2.getType1());
    21                     return index_1 - index_2;
    22                 };
    23             });
    24             return list;
    25         }else {
    26             return null;
    27         }
    28     }
    29     
    30     @Test
    31     public void run() {
    32         DlgyIndexYearExample dlgyIndexYearExample = new DlgyIndexYearExample();
    33         Criteria createCriteria = dlgyIndexYearExample.createCriteria();
    34         createCriteria.andPdateEqualTo("2020");
    35         
    36         List<DlgyIndexYear> selectByExample = dlgyIndexYearMapper.selectByExample(dlgyIndexYearExample);
    37         
    38         TestCollection testCollection = new TestCollection();
    39         List<DlgyIndexYear> sort = testCollection.sort(selectByExample);
    40         
    41         for (DlgyIndexYear dlgyIndexYear : sort) {
    42             System.out.println(dlgyIndexYear.toString());
    43         }
    44     }
    45 }
  • 相关阅读:
    UVA748 Exponentiation 解题报告
    uva465 解题报告
    UVA 10494 If We Were a Child Again 解题报告
    【今日所得】1.29。。。
    题型总结之高精度
    cf 163 div2 c题 YY 思路
    寒假计划
    Facebook Hacker Cup 2013 Qualification Round 解题报告
    戴戒指的含义(以后要结婚的必看)
    男人心疼女人的13种方式
  • 原文地址:https://www.cnblogs.com/gdou/p/12484707.html
Copyright © 2020-2023  润新知