1、引入pom依赖
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
2、自定义工具类
package com.tongtu.lzz.utils;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.util.StringUtils;
public class PinyinUtils {
static String[] InitialConsonant = {"zh","ch","sh"};
/**
* 汉字字符串获取第n个汉字的全拼
* @param characters
* @param n
* @return
*/
public static String CharacterToPinyin(String characters,int n){
String pinyin = null;
if(null == characters){
return "";
}
characters = characters.trim();
characters = characters.replaceAll(" ", "");
if("".equals(characters)){
return "";
}
char c = characters.charAt(n);
boolean fg = isChineseChar(String.valueOf(c));
if(!fg){
return "";
}
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
pinyin = pinyins[0];
return pinyin;
}
/**
* 汉字字符串获取第n个汉字的声母+声调,声调用数字表示;1:一声、2:二声、3:三声、4:四声。如:赵匡胤 返回 zh4
* @param characters
* @param n
* @return
*/
public static String CharacterToInitialConsonantWithTone(String characters,int n){
if(null == characters){
return "";
}
String initialConsonant = "";
String tone = "";
characters = characters.trim();
characters = characters.replaceAll(" ", "");
if("".equals(characters)){
return "";
}
char c = characters.charAt(n);
boolean fg = isChineseChar(String.valueOf(c));
if(!fg){
return "";
}
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
String pinyin = pinyins[0];
if(StringUtils.hasText(pinyin)){
tone = pinyin.substring(pinyin.length()-1);
}
for (int i = 0; i < InitialConsonant.length; i++) {
String string = InitialConsonant[i];
if(0 == pinyin.indexOf(string)){
initialConsonant = string;
break;
}
}
if(!StringUtils.hasText(initialConsonant)){
initialConsonant = pinyin.substring(0, 1);
}
return initialConsonant+tone;
}
/**
* 汉字字符串获取全拼+声调
* @param characters
* @return
*/
public static String CharactersToPinyinWithTones(String characters){
String pinyinFull = "";
if(null == characters){
return pinyinFull;
}
characters = characters.trim();
characters = characters.replaceAll(" ", "");
if("".equals(characters)){
return pinyinFull;
}
boolean fg = isChineseChar(characters);
if(!fg){
return pinyinFull;
}
int len = characters.length();
for (int i = 0; i < len; i++) {
char c = characters.charAt(i);
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
String pinyin = pinyins[0];
if(StringUtils.hasText(pinyin)){
pinyinFull += pinyin;
}
}
return pinyinFull;
}
/**
* 判断一个字符是否是汉字
* 中文汉字的编码范围:[\u4e00-\u9fa5]
*
* @param c 需要判断的字符
* @return 是汉字(true), 不是汉字(false)
*/
public static boolean isChineseChar(String c) {
return c.matches("[\\u4E00-\\u9FA5]+");
}
}
3、使用
//排序
if (result.size() > 0) {
Collections.sort(result, new Comparator<ZGYPullVo>() {
@Override
public int compare(ZGYPullVo o1, ZGYPullVo o2) {
String un1 = o1.getName();
String un2 = o2.getName();
String pinyin1 = PinyinUtils.CharactersToPinyinWithTones(un1);
String pinyin2 = PinyinUtils.CharactersToPinyinWithTones(un2);
if (!StringUtils.hasText(pinyin1)) {
pinyin1 = "zzzzz4";
}
if (!StringUtils.hasText(pinyin2)) {
pinyin2 = "zzzzz4";
}
int ct = pinyin1.compareToIgnoreCase(pinyin2);
return ct;
}
});
}