排序规则:人员在线状态 -> 按首字符升序: 英文(不区分大小写)-> 中文-> 数字-> 特殊字符。
代码
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.vo.EmployeeVO;
import java.text.Collator;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
/**
* 1排前面
* -1排后面
*
* @author jason
*/
public class SortNewMain {
public static void main(String[] args) {
List<EmployeeVO> list = CollectionUtil.newArrayList();
list.add(new EmployeeVO().setName("B测试").setOnLine(false));
list.add(new EmployeeVO().setName("a测试").setOnLine(false));
list.add(new EmployeeVO().setName("233").setOnLine(false));
list.add(new EmployeeVO().setName("1").setOnLine(false));
list.add(new EmployeeVO().setName("中文").setOnLine(false));
list.add(new EmployeeVO().setName("阿布").setOnLine(false));
list.add(new EmployeeVO().setName("^%$#").setOnLine(true));
list.add(new EmployeeVO().setName("&").setOnLine(false));
System.out.println("排序前:");
System.out.println(JSONUtil.toJsonStr(list));
// 在线状态 -> 按首字母升序,先英文(不区分大小写),然后中文,然后数字,然后特殊字符)
list.sort(
Comparator.comparing(EmployeeVO::getOnLine).reversed()
.thenComparing(new CustomComparator(RegexPool.WORD))
.thenComparing(new CustomComparator(RegexPool.CHINESE))
.thenComparing(new CustomComparator(RegexPool.NUMBERS))
);
System.out.println("首字母排序后:");
System.out.println(JSONUtil.formatJsonStr(JSONUtil.toJsonStr(list)));
}
/**
* 首字符排序:英文-中文-数字-特殊字符
*/
public static class CustomComparator implements Comparator<EmployeeVO> {
private final String PATTERN;
public CustomComparator(String PATTERN) {
this.PATTERN = PATTERN;
}
@Override
public int compare(EmployeeVO o1, EmployeeVO o2) {
String firstLetter1 = StrUtil.sub(o1.getName(), 0, 1);
String firstLetter2 = StrUtil.sub(o2.getName(), 0, 1);
boolean b1 = ReUtil.isMatch(PATTERN, firstLetter1);
boolean b2 = ReUtil.isMatch(PATTERN, firstLetter2);
// 英文
if (StrUtil.equals(PATTERN, RegexPool.WORD) && b1 && b2) {
return Collator.getInstance(Locale.UK).compare(firstLetter1, firstLetter2);
}
// 中文
if (StrUtil.equals(PATTERN, RegexPool.CHINESE) && b1 && b2) {
return Collator.getInstance(Locale.CHINESE).compare(firstLetter1, firstLetter2);
}
// 数字
if (StrUtil.equals(PATTERN, RegexPool.NUMBERS) && b1 && b2) {
return Collator.getInstance().compare(firstLetter1, firstLetter2);
}
if (b1 ^ b2) {
return b1 ? -1 : 1;
}
return 0;
}
}
}
结果
排序前:
[
{
"name": "B测试",
"onLine": false
},
{
"name": "a测试",
"onLine": false
},
{
"name": "233",
"onLine": false
},
{
"name": "1",
"onLine": false
},
{
"name": "中文",
"onLine": false
},
{
"name": "阿布",
"onLine": false
},
{
"name": "^%$#",
"onLine": true
},
{
"name": "&",
"onLine": false
}
]
排序后:
[
{
"name": "^%$#",
"onLine": true
},
{
"name": "a测试",
"onLine": false
},
{
"name": "B测试",
"onLine": false
},
{
"name": "阿布",
"onLine": false
},
{
"name": "中文",
"onLine": false
},
{
"name": "1",
"onLine": false
},
{
"name": "233",
"onLine": false
},
{
"name": "&",
"onLine": false
}
]