@Data
static class Demo{
private String name;
public Demo(String s) {
this.name=s;
}
}
public static int sortByPinYin(String o1, String o2) {
List<String> list = Arrays.asList(o1, o2);
Collections.sort(list, Collator.getInstance(Locale.CHINA));
return list.get(0).equals(o1) ? -1 : 1;
}
public static void main(String[] args) throws ParseException {
Demo 安徽省 = new Demo("安徽省");
Demo 安庆市 = new Demo("安庆市");
Demo 合肥市 = new Demo("合肥市");
ArrayList<Demo> demos = CollectionUtil.newArrayList(合肥市, 安庆市,安徽省);
System.out.println("raw:\n");
for (Demo demo : demos) {
System.out.println(demo.getName());
}
demos.sort((o1, o2) -> BeanMapUtil.sortByPinYin(o1.getName(), o2.getName()));
System.out.println("sort:\n");
for (Demo demo : demos) {
System.out.println(demo.getName());
}
}
05-10
11-06
3646
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)