In recent projects, traditional Chinese characters need to be sorted by pinyin
public static void main(String[] args) {
Comparator cmp = ();
(arr, cmp);
for (int i = 0; i < ; i++)
(arr[i]);
(arr1, cmp);
for (int i = 0; i < ; i++)
(arr1[i]);
}
[code]
package zhouyrt;
import ;
import ;
import ;
import ;
public class PinyinPaixu {
static class Person {
private String name;
private Integer salary;//Salary
private Integer age;//year
Person(String n, Integer s, Integer a) {
= n;
= s;
= a;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
= salary;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
= age;
}
public String toString() {
return "Name:" + + "\tSalary:" + + "\tAge:" + ;
}
}
/*
* Sort by salary, from low to high
*/
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;
}
}
/*
* Sort by year, from low to high
*/
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;
}
}