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

项目中的一个实际需求,对于数据库中的信息,不同的查询要求以不同的排序方式来展示; 抽象成下面的问题进行解决;

[b]问题描述:[/b]
学校的学生信息,包含以下属性: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

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

[b]1.学生具有ID、姓名两个属性,要求先按照ID排序,ID相同的按照姓名排序;[/b]
这是一个比较典型的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
[b]需要注意的是:如果ID是String类型的话,那么是按字典顺序排列的,即:8>15[/b]

[b]2.扩展:学生的信息不限定,即:可能有所在城市的信息,也可能没有,甚至可能有联系方式等属性;[/b]
这里有一个小把戏来解决,在学生的Bean类中,添加一个HashMap用于保存学生的所有信息(这里不展示get、set以及toString方法):

//信息丰富的学生类
public class EnrichStudent
{
private String id;

private String name;

private HashMap<String, String> attributes;
}

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

[b]3.扩展:不同的学校期望以不同的方式来排序;[/b]
需要在实现的比较类中添加一个属性来保存比较的属性:

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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值