java 泛型集合排序_Java对象比较器对泛型List进行排序-Demo

JSONObject json = newJSONObject();

String dataType= request.getParameter("dataType");

String channelId= request.getParameter("channelId");

String order= request.getParameter("type");

setMapTimeRight(dataType,0);

map.put("channelId", channelId);

map.put("order", order);try{

List now = this.service

.unconventionBrandSituation(map);//1、产生组装排序条件字段数据标记

int bigIdOrder=0;int i=-1;int i2=0;

List bigIdOrderArray= newArrayList();for(UnconventionBrandAnalyseByBrand un : now){

i++;//如果当前bigId与前一个bigId相等,则将该id视为一个id(bigIdOrder),对该id所对应的所有频次数进行累加

if(i==0

&&now.get(0).getBigId().equals(now.get(1).getBigId())){//第一次计数后有(非分组条件)

bigIdOrder = bigIdOrder+Integer.parseInt(now.get(i).getPinci());

}else if(i>0

&&i

&&!now.get(i).getBigId().equals(now.get(i-1).getBigId())&&now.get(i).getBigId().equals(now.get(i+1).getBigId())){//前不同后相同(非分组条件)

bigIdOrder = bigIdOrder+Integer.parseInt(now.get(i).getPinci());

}else if(i>0

&&i

&&now.get(i).getBigId().equals(now.get(i+1).getBigId())&&now.get(i).getBigId().equals(now.get(i+1).getBigId())){//前相同后相同(非分组条件)

bigIdOrder = bigIdOrder+Integer.parseInt(now.get(i).getPinci());

}else{//分组条件

bigIdOrder = bigIdOrder+Integer.parseInt(now.get(i).getPinci());

un.setBigIdOrder(bigIdOrder+"");//添加分组标识(int类型bigIdOrder)

bigIdOrderArray.add(i2, bigIdOrder+"");

i2++;

bigIdOrder= 0;

}

}//2、补全产生组装排序条件字段数据标记

int ii = 0;int ng = 0;//非分组条件计数器

for(UnconventionBrandAnalyseByBrand un : now) {//如果当前bigId与前一个bigId相等,则将该id视为一个id(bigIdOrder),对该id所对应的所有频次数进行累加

if (ii == 0

&& !now.get(0).getBigId().equals(now.get(1).getBigId())) {//第一次计数后无(分组条件)

} else if (ii > 0

&& i < now.size() - 1

&&now.get(ii).getBigId().equals(

now.get(ii- 1).getBigId())&& !now.get(ii).getBigId().equals(

now.get(ii+ 1).getBigId())) {//前同后不同(分组条件)

} else if (ii == now.size() - 1

&&now.get(ii).getBigId().equals(

now.get(ii- 1).getBigId())) {//后无前同(分组条件)

} else if (ii == now.size() - 1

&& !now.get(ii).getBigId().equals(

now.get(ii- 1).getBigId())) {//后无前不同(分组条件)

} else{//非分组条件

if (ii == 0) {

un.setBigIdOrder(bigIdOrderArray.get(0).toString());

}else if (!now.get(ii).getBigId().equals(

now.get(ii- 1).getBigId())) {

ng++;

un.setBigIdOrder(bigIdOrderArray.get(ng).toString());

}else{

un.setBigIdOrder(bigIdOrderArray.get(ng).toString());

}

}

ii++;

}//3、开始排序采用对象比较器

Comparator comparator = new Comparator() {public intcompare(UnconventionBrandAnalyseByBrand s1,

UnconventionBrandAnalyseByBrand s2) {//排序标记

int ret = 0;if (s1.getBigIdOrder() !=s2.getBigIdOrder()) {

ret=Integer.parseInt(s1.getBigIdOrder())-Integer.parseInt(s2.getBigIdOrder());

}return ret > 0 ? -1 : 1;//升序或者降序改变-1和1的位置即可

}

};//开始自动排序

Collections.sort(now,comparator);

json.put("data", now);try{

response.setContentType("text/html;charset=UTF-8");

PrintWriter out=response.getWriter();

out.print(json);

out.close();

}catch(Exception e) {

e.printStackTrace();

}

}catch(Exception e) {

e.printStackTrace();

}return null;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值