1、引入jar包
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
2、工具类
public class SortUtil {
private static HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
//根据省份首字母排序
public static void getSortLetter(String[] oriArray) {
Arrays.sort(oriArray, new Comparator<String>() {
HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
@Override
public int compare(String oriArray1, String oriArray2) {
try {
oriArray1 = PinyinHelper.toHanYuPinyinString(oriArray1, pinyinOutputFormat, " ", true);
oriArray2 = PinyinHelper.toHanYuPinyinString(oriArray2, pinyinOutputFormat, " ", true);
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
return oriArray1.compareTo(oriArray2);
}
});
}
/**
* 根据指定字段对 List 集合进行排序。
*
* @param list 需要排序的 List 集合
* @param sortField 排序字段
*/
public static <T> List<T> sortByField(List<T> list, String sortField) {
Comparator<T> comparator = (o1, o2) -> {
String value1 = getValueByField(o1, sortField);
String value2 = getValueByField(o2, sortField);
try {
value1 = PinyinHelper.toHanYuPinyinString(value1, pinyinOutputFormat, " ", true);
value2 = PinyinHelper.toHanYuPinyinString(value2, pinyinOutputFormat, " ", true);
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
return value1.compareTo(value2);
};
Collections.sort(list, comparator);
return list;
}
/**
* 根据字段名称获取对象的属性值。
*
* @param object 对象
* @param fieldName 字段名称
* @return 字段值
*/
private static <T> String getValueByField(T object, String fieldName) {
try {
// 假设字段名称是以 "get" 开头的方法
String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Class<?> clazz = object.getClass();
Method method = clazz.getMethod(methodName);
Object value = method.invoke(object);
return value.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
3、测试demo
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import java.time.LocalDateTime;
@Data
@RequiredArgsConstructor
@AllArgsConstructor
public class Province{
private String id;
private String name;
private LocalDateTime createTime;
}
/**
* 主要看重庆市的省份首字母排序---是按照chong,还是zhong来排序
* @param args
*/
public static void main(String[] args) {
List<Province> provinceList = new ArrayList<>();
provinceList.add(new Province("1","北京市", LocalDateTime.now()));
provinceList.add(new Province("3","上海市", LocalDateTime.now()));
provinceList.add(new Province("4","河北省", LocalDateTime.now()));
provinceList.add(new Province("6","宁夏回族自治区", LocalDateTime.now()));
provinceList.add(new Province("5","广西省", LocalDateTime.now()));
provinceList.add(new Province("9","黑龙江省", LocalDateTime.now()));
provinceList.add(new Province("2","重庆市", LocalDateTime.now()));
provinceList.add(new Province("8","江西省", LocalDateTime.now()));
/**
* provinceList:需要排序的集合
* name:要根据哪个字段来排序
*/
List<Province> list = sortByField(provinceList, "name");
list.forEach(System.out::println);
}
返回结果如下