日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

异想维度 java_Java实现多字段(维度)复杂排序

發布時間:2023/12/2 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 异想维度 java_Java实现多字段(维度)复杂排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//Java 實現多字段排序

HashMap map1 = new HashMap();

map1.put("dataindex0", null);map1.put("dataindex1", 6);map1.put("dataindex2", 1.1);map1.put("id", 1);

HashMap map2 = new HashMap();

map2.put("dataindex0", "2017-12-01");map2.put("dataindex1", 5);map2.put("dataindex2", 1.2);map2.put("id", 2);

HashMap map3 = new HashMap();

map3.put("dataindex0", "2017-12-02");map3.put("dataindex1", 7);map3.put("dataindex2", 2.3);map3.put("id", 3);

HashMap map4 = new HashMap();

map4.put("dataindex0", "2017-12-02");map4.put("dataindex1", 7);map4.put("dataindex2", 3);map4.put("id", 4);

HashMap map5 = new HashMap();

map5.put("dataindex0", "2017-12-03");map5.put("dataindex1", null);map5.put("dataindex2", 9.1);map5.put("id", 5);

HashMap map6 = new HashMap();

map6.put("dataindex0", "2017-12-03");map6.put("dataindex1", 3);map6.put("dataindex2", 8.1);map6.put("id", 6);

List> list = new ArrayList>();

list.add(map6);list.add(map1);list.add(map5);list.add(map4);list.add(map2);list.add(map3);

//?????????System.out.println("Before Sort: " + list);

final String[] orderByCols = {"dataindex0","dataindex1","dataindex2"};

//?????????final String[] orderByCols = {"dataindex0","dataindex1"};

//?????????final String[] orderByCols = {"dataindex0"};

//Mannual OrderBy

Collections.sort(list,new Comparator() {

@Override

public int compare(Map o1, Map o2) {

return recursion(o1, o2, 0);

}

private int recursion(Map o1, Map o2, int i) {

if (o1.containsKey(orderByCols[i]) && o2.containsKey(orderByCols[i])) {

Object value1 = o1.get(orderByCols[i]);

Object value2 = o2.get(orderByCols[i]);

if (value1 == null && value2 == null) {

if ((i+1) < orderByCols.length) {

int recursion = recursion(o1, o2, i+1);

return recursion;

}else{

return 0;

}

}else if(value1 == null && value2 != null){

return 1;

}else if(value1 != null && value2 == null){

return -1;

}else{

if (value1.equals(value2)) {

if ((i+1) < orderByCols.length) {

return recursion(o1, o2, i+1);

}else{

return 0;

}

}else{

if (value1 instanceof String && value2 instanceof String) {

return value1.toString().compareTo(value2.toString());

}else{

return new BigDecimal(value1.toString()).compareTo(new BigDecimal(value2.toString()));

}

}

}

}else{

System.out.println(" ** The current map do not containskey : " + orderByCols[i] + ",or The value of key is null **");

return 0;

}

}

});

總結

以上是生活随笔為你收集整理的异想维度 java_Java实现多字段(维度)复杂排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。