Java排序(灵活定义排序策略)

编程技术  /  houtizong 发布于 3年前   74
项目中的一个实际需求,对于数据库中的信息,不同的查询要求以不同的排序方式来展示; 抽象成下面的问题进行解决;

问题描述:
学校的学生信息,包含以下属性:ID、姓名、所在城市、成绩;
所有学校的学生信息统一存放,但不同学校查询信息时要求按照不同的排序策略显示,如何编写统一的排序方法满足这种需求;
学生信息如下所示(简单起见,保存在一个properties文件中):
记录=ID,姓名,所在城市,成绩
stu01=001,zhangsan,xian,90
stu02=002,zhangsan,nanjing,85
stu03=002,zhangsan,beijing,90
stu04=002,lisi,shanghai,90
stu05=003,wangwu,guangdong,95
stu06=004,qianqi,xinjiang,60

问题解决:
将问题分解为下面散步来循序渐进的解决;

1.学生具有ID、姓名两个属性,要求先按照ID排序,ID相同的按照姓名排序;
这是一个比较典型的java排序问题,首先创建一个CommonStudent的Bean类;
实现比较算法如下:
/** * 普通学生的比较类 * 先根据id排序,后根据name排序 */import java.util.Comparator;import com.crystalcoding.beans.CommonStudent;@SuppressWarnings("rawtypes")public class CommonComp implements Comparator{    public int compare(Object obj0, Object obj1)    {        CommonStudent s0 = (CommonStudent) obj0;        CommonStudent s1 = (CommonStudent) obj1;        //先比较ID的大小        int compId = s0.getId().compareTo(s1.getId());        //三段式返回        return 0 != compId ? compId : s0.getName().compareTo(s1.getName());    }}

排序结果如下:
No.0: id = 001 name = zhangsan
No.1: id = 002 name = lisi
No.2: id = 002 name = zhangsan
No.3: id = 002 name = zhangsan
No.4: id = 003 name = wangwu
No.5: id = 004 name = qianqi
需要注意的是:如果ID是String类型的话,那么是按字典顺序排列的,即:8>15

2.扩展:学生的信息不限定,即:可能有所在城市的信息,也可能没有,甚至可能有联系方式等属性;
这里有一个小把戏来解决,在学生的Bean类中,添加一个HashMap用于保存学生的所有信息(这里不展示get、set以及toString方法):
//信息丰富的学生类public class EnrichStudent{    private String id;    private String name;    private HashMap<String, String> attributes;}

仍旧使用问题1当中的Compare方法即可实现;

3.扩展:不同的学校期望以不同的方式来排序;
需要在实现的比较类中添加一个属性来保存比较的属性:
package com.crystalcoding.sort.test;import java.util.Comparator;import java.util.List;import com.crystalcoding.beans.EnrichStudent;@SuppressWarnings("rawtypes")public class ComplicateComp implements Comparator{    //用于保存比较的属性列表    private List<String> keys;        //比较类的构造函数    public ComplicateComp(List<String> keys)    {        this.keys = keys;    }        public int compare(Object obj0, Object obj1)    {        EnrichStudent s0 = (EnrichStudent) obj0;        EnrichStudent s1 = (EnrichStudent) obj1;        //属性列表遍历        for (int i = 0; i < keys.size(); i++)        {            String key = keys.get(i);            String value0 = s0.getAttributes().get(key);            String value1 = s1.getAttributes().get(key);            int compResult = value0.compareTo(value1);            if (0 != compResult)            {                return compResult;            }            else            {                if (i == keys.size() - 1)                {                    return 0;                }            }        }        return 0;    }}

该比较类调用如下所示(更加通俗的排序):
    public static void main(String[] args) throws IOException    {        //普通情况的排序        List<CommonStudent> commonList = initCommonList();        Collections.sort(commonList, new CommonComp());        print(commonList);                //信息丰富的学生排序        List<EnrichStudent> enrichList = initComplicateList();        Collections.sort(enrichList, new EnrichComp());        print(enrichList);                //更加通俗的排序        List<EnrichStudent> complicateList = initComplicateList();        List<String> keys = new ArrayList<String>();        keys.add("id");        keys.add("name");        keys.add("count");        Collections.sort(complicateList, new ComplicateComp(keys));        print(complicateList);    }

展示了其中的一种排序策略:按照ID、姓名、成绩排序,结果如下:
No.0: id = 001 name = zhangsan attributes = {id=001, count=90, address=xian, name=zhangsan}
No.1: id = 002 name = lisi attributes = {id=002, count=90, address=shanghai, name=lisi}
No.2: id = 002 name = zhangsan attributes = {id=002, count=85, address=nanjing, name=zhangsan}
No.3: id = 002 name = zhangsan attributes = {id=002, count=90, address=beijing, name=zhangsan}
No.4: id = 003 name = wangwu attributes = {id=003, count=95, address=guangdong, name=wangwu}
No.5: id = 004 name = qianqi attributes = {id=004, count=60, address=xinjiang, name=qianqi}

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客