java中实现汉字按照拼音排序(示例代码)
最近項目中需要對繁體字按拼音進行排序
public static void main(string[] args) {
comparator cmp = collator.getinstance(java.util.locale.china);
string[] arr = { "张三", "李四", "王五", "刘六" ,"周濤","戴笠","戴阿"};
string[] arr1 = {"生活","文教","政治","自然","宗教","經貿","軍事","经贸"};
//對簡體字有效,戴阿、戴笠在第一個字相同的情況下按拼音比較第二個字,很智能哦
arrays.sort(arr, cmp);
for (int i = 0; i < arr.length; i++)
//輸出:戴阿、戴笠、李四、刘六、王五、张三、周濤
system.out.println(arr[i]);
//對繁體字無效,繁體的“軍事”被拍到了最后
arrays.sort(arr1, cmp);
for (int i = 0; i < arr1.length; i++)
//輸出:经贸、生活、文教、政治、自然、宗教、經貿、軍事
system.out.println(arr1[i]);
}
關于繁體字按拼音排序,可以利用util包下的comparator接口,按自定義的規則實現compare方法即可。以下是個例子:
package zhouyrt;
import java.util.arraylist;
import java.util.arrays;
import java.util.comparator;
import java.util.list;
public class pinyinpaixu {
static class person {
private string name;
private integer salary;//薪水
private integer age;//年齡
person(string n, integer s, integer a) {
this.name = n;
this.salary = s;
this.age = a;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public integer getsalary() {
return salary;
}
public void setsalary(integer salary) {
this.salary = salary;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
public string tostring() {
return "姓名:" + this.name + "\t薪水:" + this.salary + "\t年齡:" + this.age;
}
}
/*
* 按薪水排序,從低到高
*/
static class salarycomparator implements comparator {
public int compare(object o1, object o2) {
integer salary1 = ((person)o1).salary;
integer salary2 = ((person)o2).salary;
if(salary1 - salary2 > 0)
return 1;
if(salary1 - salary2 < 0)
return -1;
else
return 0;
}
}
/*
* 按年齡排序,從低到高
*/
static class agecomparator implements comparator {
public int compare(object o1, object o2) {
integer age1 = ((person)o1).age;
integer age2 = ((person)o2).age;
if(age1 - age2 > 0)
return 1;
if(age1 - age2 < 0)
return -1;
else
return 0;
}
}
main方法測試:
public static void main(string[] args) {
list<person> list = new arraylist<person>();
list.add(new person("張洋",3400,25));
list.add(new person("王肅",10000,27));
list.add(new person("李安",9000,30));
list.add(new person("劉虎",2500,22));
list.add(new person("安德剛",3500,21));
person[] personary = new person[list.size()];
personary = list.toarray(new person[list.size()]);
system.out.println("--- 排序前 ------------------------------------");
for(person p : personary) {
system.out.println(p);
}
arrays.sort(personary,new salarycomparator());
system.out.println("--- 按薪水排序后 ------------------------------------");
for(person p : personary) {
system.out.println(p);
}
arrays.sort(personary,new agecomparator());
system.out.println("--- 按年齡排序后 ------------------------------------");
for(person p : personary) {
system.out.println(p);
}
}
}
以下是輸出結果:
--- 排序前 ------------------------------------
姓名:張洋 薪水:3400 年齡:25
姓名:王肅 薪水:10000 年齡:27
姓名:李安 薪水:9000 年齡:30
姓名:劉虎 薪水:2500 年齡:22
姓名:安德剛 薪水:3500 年齡:21
--- 按薪水排序后 ------------------------------
姓名:劉虎 薪水:2500 年齡:22
姓名:張洋 薪水:3400 年齡:25
姓名:安德剛 薪水:3500 年齡:21
姓名:李安 薪水:9000 年齡:30
姓名:王肅 薪水:10000 年齡:27
--- 按年齡排序后 -------------------------------
姓名:安德剛 薪水:3500 年齡:21
姓名:劉虎 薪水:2500 年齡:22
姓名:張洋 薪水:3400 年齡:25
姓名:王肅 薪水:10000 年齡:27
姓名:李安 薪水:9000 年齡:30