由于最近接口中有一些数据是从MySQL中查询出来的,使用Rest请求返回的数据是List<map<string,object>>类型。
原始数据返回结构:
1.需求的来源
如下返回的resourceId需要进行排序,呵呵,能排序,就是比较麻烦点,我找机会参阅资料总结一下吧。
{
"total": 15,
"data": [
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1",
"resourceName": "4-OPCOM100-CPM12A"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=4#portType=263",
"resourceName": "4-C/1331(波道4)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=5#portType=263",
"resourceName": "4-C/1351(波道5)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=3#portType=263",
"resourceName": "4-C/1311(波道3)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=7#portType=263",
"resourceName": "4-C/1471(波道11)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=6#portType=263",
"resourceName": "4-C/1371(波道6)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=1#portType=263",
"resourceName": "4-C/1271(波道1)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=10#portType=263",
"resourceName": "4-C/1531(波道14)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=1#portType=262",
"resourceName": "4-Pri"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=8#portType=263",
"resourceName": "4-C/1491(波道12)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=9#portType=263",
"resourceName": "4-C/1511(波道13)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=12#portType=263",
"resourceName": "4-C/1571(波道16)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=11#portType=263",
"resourceName": "4-C/1551(波道15)"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=2#portType=262",
"resourceName": "4-Sec"
},
{
"leaf": "true",
"resourceId": "/ne=100007/shelf=1/slot=4/card=1.1/port=2#portType=263",
"resourceName": "4-C/1291(波道2)"
}
]
}
看着比较蛋疼了 。
2.编写测试类
如下不太实用【String进行排序是按照字母表顺序的】,因为如下的count是整数,对整数进行排序的,要想正确排序,需要找到规律,进行特殊处理。
该代码是按照map的value排序了。
package com.raisecom.tiap.ems.basic.mgt.controller.wdm.semiactive;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author yangl-006305
* @version V1.0
* @date 2020-08-15 17:52
*/
public class SortListHashMap {
public static void main(String[] args) {
List<Map<String, Object>> listResult = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new LinkedHashMap<String, Object>();
map1.put("count", 2);
map1.put("name", "abc");
map1.put("key", "acv");
listResult.add(map1);
Map<String, Object> map2 = new LinkedHashMap<String, Object>();
map2.put("count", 3);
map2.put("name", "bbc");
map2.put("key", "bcv");
listResult.add(map2);
Map<String, Object> map3 = new LinkedHashMap<String, Object>();
map3.put("count", 1);
map3.put("name", "cbc");
map3.put("key", "ccv");
listResult.add(map3);
Map<String, Object> map4 = new LinkedHashMap<String, Object>();
map4.put("count", 4);
map4.put("name", "cbc");
map4.put("key", "ccv");
listResult.add(map4);
System.out.println("排序前:");
for (Map<String, Object> map : listResult) {
System.out.println("count:"+map.get("count")+" name:"+map.get("name")+" key:"+map.get("key"));
}
Collections.sort(listResult, new MapComparatorDesc());
System.out.println("降序:");
for (Map<String, Object> map : listResult) {
System.out.println("count:"+map.get("count")+" name:"+map.get("name")+" key:"+map.get("key"));
}
Collections.sort(listResult, new MapComparatorAsc());
System.out.println("升序:");
for (Map<String, Object> map : listResult) {
System.out.println("count:"+map.get("count")+" name:"+map.get("name")+" key:"+map.get("key"));
}
}
static class MapComparatorDesc implements Comparator<Map<String, Object>> {
@Override
public int compare(Map<String, Object> m1, Map<String, Object> m2) {
Integer v1 = Integer.valueOf(m1.get("count").toString());
Integer v2 = Integer.valueOf(m2.get("count").toString());
if (v2 != null) {
return v2.compareTo(v1);
}
return 0;
}
}
static class MapComparatorAsc implements Comparator<Map<String, Object>> {
@Override
public int compare(Map<String, Object> m1, Map<String, Object> m2) {
Integer v1 = Integer.valueOf(m1.get("count").toString());
Integer v2 = Integer.valueOf(m2.get("count").toString());
if(v1 != null){
return v1.compareTo(v2);
}
return 0;
}
}
}