集合排序

96 阅读1分钟

1.JSONArray

(1)根据某个属性升序

JSONArray jsonArray = result.getJSONArray("data");
// 按照上传时间升序排序
jsonArray.sort(Comparator.comparing(obj->((JSONObject)obj).getString("time")));

(2) 根据某个属性降序

jsonArray.sort(Comparator.comparing(obj->((JSONObject)obj).getString("time")).reversed());

2.List 集合

// 从map获取deviceInfoList
        List<DeviceInfo> deviceInfoList = mapDeviceInfo.entrySet().stream().map(e -> e.getValue()).collect(Collectors.toList());
        // 按照getDeviceId 升序排序
        deviceInfoList = deviceInfoList.stream().sorted(Comparator.comparing(DeviceInfo::getDeviceId)).collect(Collectors.toList());

3.应用案例