最近项目中有新的需求,需要对包含数字、字母、中文的字符串按首字母排序。看似简单,但是实现起来却很难。因为这里面包含了中文,不能单纯的取首字母的ASSIC值进行比较。考虑到最终是根据英文字母进行比较的,索性将字符串统统转化成拼音字符串进行处理。具体的代码如下。

PinyinUtils.javapackage com.fangshuo;

import net.sourceforge.pinyin4j.PinyinHelper;

import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;

import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;

import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**

* 汉字转换为拼音

* @author Red

*/

public class PinyinUtils {

/**

* 获取字符串拼音的第一个字母

* @param chinese

* @return

*/

public static String ToFirstChar(String chinese){

String pinyinStr = "";

char[] newChar = chinese.toCharArray(); //转为单个字符

HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

for (int i = 0; i < newChar.length; i++) {

if (newChar[i] > 128) {

try {

pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);

} catch (BadHanyuPinyinOutputFormatCombination e) {

e.printStackTrace();

}

}else{

pinyinStr += newChar[i];

}

}

return pinyinStr;

}

/**

* 汉字转为拼音

* @param chinese

* @return

*/

public static String ToPinyin(String chinese){

String pinyinStr = "";

char[] newChar = chinese.toCharArray();

HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

for (int i = 0; i < newChar.length; i++) {

if (newChar[i] > 128) {

try {

pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];

} catch (BadHanyuPinyinOutputFormatCombination e) {

e.printStackTrace();

}

}else{

pinyinStr += newChar[i];

}

}

return pinyinStr;

}

/**

* 汉字转为拼音并获取大写的首字母;

* @param chinese

* @return

*/

public static String ToPinyinAndGetFirstChar(String chinese){

String pinyinStr = "";

char[] newChar = chinese.toCharArray();

HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

for (int i = 0; i < newChar.length; i++) {

if (newChar[i] > 128) {

try {

pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];

} catch (BadHanyuPinyinOutputFormatCombination e) {

e.printStackTrace();

}

}else{

pinyinStr += newChar[i];

}

}

return pinyinStr.toUpperCase();

}

}

测试载体Students.java

package com.fangshuo;

public class Students {

private int age;

private int score;

private String name;

public Students() {

super();

}

public Students(int age, int score, String name) {

super();

this.age = age;

this.score = score;

this.name = name;

}

public Students(int age, int score){

super();

this.age = age;

this.score = score;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

测试类:

package com.fangshuo;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class SortTest {

public static void main(String[] args) {

List students = new ArrayList();

students.add(new Students(23, 100,"阿里azj001"));

students.add(new Students(27, 98,"腾讯bzj002"));

students.add(new Students(29, 99,"百度dzj003"));

students.add(new Students(29, 98,"华为czj004"));

students.add(new Students(22, 89,"择天ezj005"));

students.add(new Students(22, 89,"a择天ezj005"));

students.add(new Students(22, 89,"A择天ezj005"));

students.add(new Students(22, 89,"8择天ezj005"));

students.add(new Students(22, 89,"0择天ezj005"));

students.add(new Students(22, 89,"z择天ezj005"));

students.add(new Students(22, 89,"_z择天ezj005"));

Collections.sort(students, new Comparator() {

@Override

public int compare(Students o1, Students o2) {

Students newo1 = new Students();

newo1.setName(PinyinUtils.ToPinyinAndGetFirstChar(o1.getName()));

Students newo2 = new Students();

newo2.setName(PinyinUtils.ToPinyinAndGetFirstChar(o2.getName()));

int i = newo1.getName().substring(0, 1).compareTo(newo2.getName().substring(0, 1));

return i;

}

});

for(Students stu : students){

System.out.println("score:" + stu.getScore() + ":age" + stu.getAge()+":name:"+stu.getName());

}

}

}

执行结果如下:

de1e67e9862142901720304d14b06bbc.gif

知识点:

字符串比较大小,可以使用现有的工具类:int result = str1.compareTo(str2);该方法返回两者的ASSIC 码的差值;

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐