指定排序增删改的时候先过滤再重组
package com.bosscloud.form.main.utils;import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;/*** 指定排序增删改的时候先过滤再重组**/
public class JsonUtils {
/*** 增删改的时候*/@Testpublic void sortBySpecifiedFieldByString() {JSONObject param0 = new JSONObject();param0.set("__type", "insert");JSONObject param1 = new JSONObject();param1.set("__type", "insert");JSONObject param2 = new JSONObject();param2.set("__type", "update");JSONObject param3 = new JSONObject();param3.set("__type", "delete");JSONArray paramArray = new JSONArray();paramArray.add(param0);//paramArray.add(param1);//paramArray.add(param2);paramArray.add(param3);System.out.println("原始数据:" + paramArray.toString());//paramArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getStr("__type")));//System.out.println("排序(升序):" + paramArray.toString());//paramArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getStr("__type")).reversed());//System.out.println("排序(降序):" + paramArray.toString());//原始数据:[{"type":"insert"},{"type":"update"},{"type":"delete"}]//排序(升序):[{"type":"delete"},{"type":"insert"},{"type":"update"}]//排序(降序):[{"type":"update"},{"type":"insert"},{"type":"delete"}]//指定排序//sortBySpecifiedFieldByString(paramArray);JSONArray jsonArray = JsonUtils.sortBySpecifiedFieldByStr(paramArray);System.out.println("排序(调整):" + jsonArray);}/*** 指定排序增删改的时候先过滤再重组* <p>* 应该是先过滤再重组* 原始数据:[{"__type":"insert"},{"__type":"insert"},{"__type":"update"},{"__type":"delete"}]* 排序(调整):[[{"__type":"delete"}],[{"__type":"update"}],[{"__type":"insert"},{"__type":"insert"}]]** @param RsqDSN* @return*/public static JSONArray sortBySpecifiedFieldByStr(JSONArray RsqDSN) {cn.hutool.json.JSONArray pObjDelete = new cn.hutool.json.JSONArray();cn.hutool.json.JSONArray pObjUpdate = new cn.hutool.json.JSONArray();cn.hutool.json.JSONArray pObjInsert = new cn.hutool.json.JSONArray();cn.hutool.json.JSONArray pObjSelect = new cn.hutool.json.JSONArray();for (int idx = 0; idx < RsqDSN.size(); idx++) {cn.hutool.json.JSONObject RsqDSN_i = (cn.hutool.json.JSONObject) RsqDSN.get(idx);String sqlExec = (String) RsqDSN_i.get(AppUtil.DOUBLE_UNDERLINE__TYPE);if (SqlExecEnum.TYPE_DELETE.getCode().equals(sqlExec)) {pObjDelete.put(RsqDSN_i);} else if (SqlExecEnum.TYPE_UPDATE.getCode().equals(sqlExec)) {pObjUpdate.put(RsqDSN_i);} else if (SqlExecEnum.TYPE_INSERT.getCode().equals(sqlExec)) {pObjInsert.put(RsqDSN_i);} else {pObjSelect.put(RsqDSN_i);}}JSONArray pArrayAll = new JSONArray();if (ObjectUtil.isNotNull(pObjDelete) && pObjDelete.size() > 0) {pArrayAll.addAll(pObjDelete);}if (ObjectUtil.isNotNull(pObjUpdate) && pObjUpdate.size() > 0) {pArrayAll.addAll(pObjUpdate);}if (ObjectUtil.isNotNull(pObjInsert) && pObjInsert.size() > 0) {pArrayAll.addAll(pObjInsert);}if (ObjectUtil.isNotNull(pObjSelect) && pObjSelect.size() > 0) {pArrayAll.addAll(pObjSelect);}return pArrayAll;}
}
最终结果
原始数据:
[{“__type”:“insert”},{“__type”:“insert”},{“__type”:“update”},{“__type”:“delete”}]
排序(调整):
[{“__type”:“delete”},{“__type”:“update”},{“__type”:“insert”},{“__type”:“insert”}]