借鉴网络其他文章,将汉字拼音和数字排序整合为一体, 代码或许有些冗余,但已是菜鸟的最大努力
效果
实体类
package model;
public class PinYin {
public String name;
public Integer age;
public String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public PinYin() {
}
public PinYin(String name, Integer age, String type) {
this.name = name;
this.age = age;
this.type = type;
}
@Override
public String toString() {
return "PinYin{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
}
}
main方法
package study;
import model.PinYin;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PinYinSort {
private static Pattern PATTERN = Pattern.compile("-?[0-9]+.?[0-9]*");
public static void main(String[] args) {
List<PinYin> pinyins = init();
System.out.println("排序前: ");
for (PinYin pinYin : pinyins) {
System.out.println(pinYin);
}
pinyins = sortByName(pinyins);
System.out.println("排序之后:");
for (PinYin pinYin : pinyins) {
System.out.println(pinYin);
}
}
/**
* 仅按拼音排序
* @param pinyins
* @return
*/
// public static List<PinYin> sortByName(List<PinYin> pinyins) {
// final HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
// Collections.sort(pinyins, new Comparator<PinYin>() {
//
// public int compare(PinYin o1, PinYin o2) {
// String pinyin1 = null;
// String pinyin2 = null;
// try {
// pinyin1 = PinyinHelper.toHanYuPinyinString(o1.getName(), pinyinOutputFormat, " ", true);
// pinyin2 = PinyinHelper.toHanYuPinyinString(o2.getName(), pinyinOutputFormat, " ", true);
// } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
// badHanyuPinyinOutputFormatCombination.printStackTrace();
// }
// return pinyin1.compareTo(pinyin2);
// }
// });
// return pinyins;
// }
public static List<PinYin> sortByName(List<PinYin> pinyins) {
final HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
//名称排序
Collections.sort(pinyins, new Comparator<PinYin>() {
public int compare(PinYin o1, PinYin o2) {
//判空
if (o1 == null || o1.getName() ==null) return 1;
if (o2 == null || o2.getName() ==null) return -1;
if (o1.getName().equals(o2.getName())) return 0;
String pinyin1 = null;
String pinyin2 = null;
//拼音排序
try {
pinyin1 = PinyinHelper.toHanYuPinyinString(o1.getName(), pinyinOutputFormat, " ", true);
pinyin2 = PinyinHelper.toHanYuPinyinString(o2.getName(), pinyinOutputFormat, " ", true);
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
//拼音后数字排序
int index = 0;
Integer i1 = 0;
String s1 = "";
/*
1.以字符串的最后一位为准,向前筛选;
2.判断index的当前字符是否属于数字,否则跳出
3.向后截取字符串并转为数字
4.将数字的部分删掉
*/
for (index = pinyin1.length() - 1; index >= 0; index --) {
Matcher matcher = PATTERN.matcher(String.valueOf(pinyin1.charAt(index)));
if (!matcher.matches()) {
break;
}
i1 = Integer.valueOf(pinyin1.substring(index));
s1 = pinyin1.replace(i1+"", "");
}
Integer i2 = 0;
String s2 = "";
for (index = pinyin2.length() - 1; index >= 0; index --) {
Matcher matcher = PATTERN.matcher(String.valueOf(pinyin2.charAt(index)));
if (!matcher.matches()) {
break;
}
i2 = Integer.valueOf(pinyin2.substring(index));
s2 = pinyin2.replace(i2+"", "");
}
//如果字符相同,则比较数字大小
if (s1.equals(s2)) {
return i1 - i2;
}
//字符比较
return s1.compareToIgnoreCase(s2);
}
});
return pinyins;
}
public static List<PinYin> init() {
List<PinYin> list = new ArrayList<PinYin>();
PinYin pinYin = new PinYin("安安1", 18, "tra");
PinYin pinYin1 = new PinYin("安安21", 18, "tb");
PinYin pinYin2 = new PinYin("安安3", 18, "tra");
PinYin pinYin3 = new PinYin("韩梅梅17", 18, "ft");
PinYin pinYin4 = new PinYin("赵冬梅24", 18, "tesb");
PinYin pinYin5 = new PinYin("安安", 18, "tesa");
PinYin pinYin6 = new PinYin("安安11", 18, "tesb");
PinYin pinYin7 = new PinYin("韩梅梅1", 18, "tesb");
PinYin pinYin8 = new PinYin("赵冬梅", 18, "tesb");
list.add(pinYin);
list.add(pinYin1);
list.add(pinYin2);
list.add(pinYin3);
list.add(pinYin4);
list.add(pinYin5);
list.add(pinYin6);
list.add(pinYin7);
list.add(pinYin8);
return list;
}
}
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pinyinSort</groupId>
<artifactId>pinyinSort</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
</project>