|
@@ -1,9 +1,9 @@
|
|
|
package com.persagy.dmp.middleware.utils;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
+import java.text.CollationKey;
|
|
|
import java.text.Collator;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -58,31 +58,15 @@ public class SortUtil {
|
|
|
*/
|
|
|
public static void sortByAlphabetical(List<JSONObject> list, String name) {
|
|
|
Collections.sort(list, new Comparator<JSONObject>() {
|
|
|
+ Collator collator = Collator.getInstance(Locale.CHINA);
|
|
|
@Override
|
|
|
public int compare(JSONObject o1, JSONObject o2) {
|
|
|
- return arrangeStringsAccordingToAscii(o1.getString(name), o2.getString(name));
|
|
|
+ CollationKey key1 = collator.getCollationKey(o1.getString(name));
|
|
|
+ CollationKey key2 = collator.getCollationKey(o2.getString(name));
|
|
|
+ return key1.compareTo(key2);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * ascii编码排序
|
|
|
- * @param firstText
|
|
|
- * @param lastText
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static int arrangeStringsAccordingToAscii(String firstText, String lastText) {
|
|
|
- char[] firstChars = firstText.toCharArray();
|
|
|
- char[] lastChars = lastText.toCharArray();
|
|
|
- int min = Math.min(firstChars.length, lastChars.length);
|
|
|
- for (int index = 0; index < min; index++) {
|
|
|
- if (firstChars[index] > lastChars[index]) {
|
|
|
- return 1;
|
|
|
- } else if (firstChars[index] < lastChars[index]) {
|
|
|
- return-1;
|
|
|
- }
|
|
|
- }
|
|
|
- return firstChars.length - lastChars.length;
|
|
|
- }
|
|
|
|
|
|
}
|