zoukankan      html  css  js  c++  java
  • 实现场景:集合对象按照自定义规则排序【代码】

    原理:

      利用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 }
  • 相关阅读:
    Java并发编程基本概念
    详解TCP:顺序和丢包问题
    详解TCP:三次握手、四次挥手
    使用DockerFile构建运行GoWeb
    Go之Gorm和BeegoORM简介及配置使用
    Nginx WebUI管理
    Kibana配置nginx反代并本地ca加密nginx
    07 . ELK Stack7.2一键多机部署脚本
    腾讯蓝鲸自动化运维平台简介部署及常见报错解决
    Go操作Redis
  • 原文地址:https://www.cnblogs.com/gdou/p/12484707.html
Copyright © 2011-2022 走看看