中文排序比较器定义
/**
* 按照Windows排序风格,对给定的数字、字母、汉字字符串进行排序
*/
public class ChineseComparator implements Comparator<String>, Serializable {
private static final long serialVersionUID = 1L;
// 数字类型
private static final Integer TYPE_NUMBER = 0;
// 字符类型(非数字)
private static final Integer TYPE_CHARACTER = 1;
/**
* 构造
*/
public ChineseComparator() {
}
@Override
public int compare(String o1, String o2) {
// 把字符串拆分成字符数组
String[] o1Chars = o1.split("");
String[] o2Chars = o2.split("");
// 根据字符数组生成带分类的字符列表
// List<Object>的第一位为该字符的类型(TYPE_NUMBER, TYPE_CHARACTER)
// List<Object>的第二位为该字符的内容(一位数字, 一位非数字, 多位数字)
List<List<Object>> o1CharList = getCharList(o1Chars);
List<List<Object>> o2CharList = getCharList(o2Chars);
// 统一CharList的长度
int max = Math.max(o1CharList.size(), o2CharList.size());
while (o1CharList.size() < max) {
o1CharList.add(new ArrayList<>());
}
while (o2CharList.size() < max) {
o2CharList.add(new ArrayList<>());
}
// 开始比较
int compare = 0;
for (int i = 0; i < max; i++) {
List<Object> o1list = o1CharList.get(i);
List<Object> o2list = o2CharList.get(i);
// CharList短的,排在前面
if (o1list.size() == 0) {
compare = -1;
break;
}
if (o2list.size() == 0) {
compare = 1;
break;
}
// 先比较类型
Integer o1Type = (Integer) o1list.get(0);
Integer o2Type = (Integer) o2list.get(0);
int typeCompare = Integer.compare(o1Type, o2Type);
if (typeCompare != 0) {
// 类型不同,则数字在前,非数字在后
compare = typeCompare;
break;
} else {
// 类型相同,则比较内容
if (TYPE_NUMBER.equals(o1Type)) {
// 比较数字
int o1Content = Integer.parseInt(o1list.get(1).toString());
int o2Content = Integer.parseInt(o2list.get(1).toString());
compare = Integer.compare(o1Content, o2Content);
} else if (TYPE_CHARACTER.equals(o1Type)) {
// 比较非数字
String o1Content = (String) o1list.get(1);
String o2Content = (String) o2list.get(1);
compare = Collator.getInstance(Locale.CHINESE).compare(o1Content, o2Content);
}
// 如果不相等,则退出比较
if (compare != 0) {
break;
}
}
}
return compare;
}
/**
* 根据字符数组生成带分类的字符列表
*
* @param chars 字符数组
* @return 带分类的字符列表,List<Object>的第一位为该字符的类型(TYPE_NUMBER, TYPE_CHARACTER),第二位为该字符的内容
*/
private List<List<Object>> getCharList(String[] chars) {
List<List<Object>> charList = new ArrayList<>();
List<Object> list;
for (int i = 0; i < chars.length; i++) {
char c = (chars[i].toCharArray())[0];
list = new ArrayList<>();
// 是否为数字
if (((int) c >= '0' && (int) c <= '9')) {
StringBuilder str = new StringBuilder();
// 下一位是否为数字,如果为数字则组成多位数
do {
str.append(c);
if (i + 1 < chars.length) {
c = (chars[++i].toCharArray())[0];
} else {
break;
}
} while ((int) c >= '0' && (int) c <= '9');
if (!(i + 1 == chars.length) || !(((int) c >= '0' && (int) c <= '9'))) {
i--;
}
list.add(TYPE_NUMBER);
list.add(str.toString());
} else {
list.add(TYPE_CHARACTER);
list.add(String.valueOf(c));
}
charList.add(list);
}
return charList;
}
}
代码示例
public static void main(String[] args) {
// List<String> list = Arrays.asList("2023-01", "2023-08", "2022-11");
// List<String> list = Arrays.asList("2", "3", "1");
// List<String> list = Arrays.asList("第3季度","第2季度","第1季度");
List<String> list = Arrays.asList("2024年-第1季度","2023年-第3季度","2022年-第4季度");
sortByChinese(list);
System.out.println(list.toString());
}
/**
* 根据中国人使用习惯进行排序(Windows风格)
*
* @param list List
* @return 排序后的List
*/
public static List<String> sortByChinese(List<String> list) {
if (list == null || list.size() < 2) {
return list;
}
list.sort(new ChineseComparator());
return list;
}
符合大多数场景排序
自然排序比较器
import java.util.*;
public class NaturalOrderComparator implements Comparator<Comparable> {
int compareRight(String a, String b) {
int bias = 0;
int ia = 0;
int ib = 0;
// The longest run of digits wins. That aside, the greatest
// value wins, but we can't know that it will until we've scanned
// both numbers to know that they have the same magnitude, so we
// remember it in BIAS.
for (; ; ia++, ib++) {
char ca = charAt(a, ia);
char cb = charAt(b, ib);
if (!Character.isDigit(ca) && !Character.isDigit(cb)) {
return bias;
} else if (!Character.isDigit(ca)) {
return -1;
} else if (!Character.isDigit(cb)) {
return +1;
} else if (ca < cb) {
if (bias == 0) {
bias = -1;
}
} else if (ca > cb) {
if (bias == 0)
bias = +1;
} else if (ca == 0 && cb == 0) {
return bias;
}
}
}
public int compare(Comparable o1, Comparable o2) {
String a = o1.toString();
String b = o2.toString();
int ia = 0, ib = 0;
int nza = 0, nzb = 0;
char ca, cb;
int result;
while (true) {
// only count the number of zeroes leading the last number compared
nza = nzb = 0;
ca = charAt(a, ia);
cb = charAt(b, ib);
// skip over leading spaces or zeros
while (Character.isSpaceChar(ca) || ca == '0') {
if (ca == '0') {
nza++;
} else {
// only count consecutive zeroes
nza = 0;
}
ca = charAt(a, ++ia);
}
while (Character.isSpaceChar(cb) || cb == '0') {
if (cb == '0') {
nzb++;
} else {
// only count consecutive zeroes
nzb = 0;
}
cb = charAt(b, ++ib);
}
// process run of digits
if (Character.isDigit(ca) && Character.isDigit(cb)) {
if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) {
return result;
}
}
if (ca == 0 && cb == 0) {
// The strings compare the same. Perhaps the caller
// will want to call strcmp to break the tie.
return nza - nzb;
}
if (ca < cb) {
return -1;
} else if (ca > cb) {
return +1;
}
++ia;
++ib;
}
}
static char charAt(String s, int i) {
if (i >= s.length()) {
return 0;
} else {
return s.charAt(i);
}
}
public static void main(String[] args) {
// String[] strings = new String[]{"1-2", "1-02", "1-20", "10-20", "fred", "jane", "pic01",
// "pic2", "pic02", "pic02a", "pic3", "pic4", "pic 4 else", "pic 5", "pic05", "pic 5",
// "pic 5 something", "pic 6", "pic 7", "pic100", "pic100a", "pic120", "pic121",
// "pic02000", "tom", "x2-g8", "x2-y7", "x2-y08", "x8-y8"};
//
// List orig = Arrays.asList(strings);
List orig=Arrays.asList("2", "3", "1");
System.out.println("Original: " + orig);
// List scrambled = Arrays.asList(strings);
// Collections.shuffle(scrambled);
System.out.println("Scrambled: " + orig);
Collections.sort(orig, new NaturalOrderComparator());
System.out.println("Sorted: " + orig);
}
}