Java to achieve Chinese characters in accordance with pinyin sorting of sample code
- 2020-04-01 02:35:41
- OfStack
Recently, we need to sort the characters by pinyin
public static void main(String[] args) {
Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
String[] arr = { " Zhang SAN ", " Li si ", " Cathy ", " Liu six " ," Weeks � "," dai "," O wear "};
String[] arr1 = {" life "," Culture and education "," political "," natural "," religious "," � � "," � things "," Economic and trade "};
//The word is effective, dai a, dai li in the first word of the same feeling under pinyin than the second word, very smart oh & PI;
Arrays.sort(arr, cmp);
for (int i = 0; i < arr.length; i++)
//Dai a, dai li, li si, liu liu, wang wu, zhang SAN, zhou zhonghe.
System.out.println(arr[i]);
//The film is full of characters and effects, and the story is filmed to the end.
Arrays.sort(arr1, cmp);
for (int i = 0; i < arr1.length; i++)
//Lessons: economy and trade, life, culture and education, politics, nature, religion, practices and practices;
System.out.println(arr1[i]);
}
Chinese characters are sorted by pinyin. Using the Comparator interface of util package, compare method can be used. Here are some examples:
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;//Salary
private Integer age;//Years �
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 " Name: " + this.name + "t Salary: " + 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 method:
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person(" � the ",3400,25));
list.add(new Person(" The king � ",10000,27));
list.add(new Person(" Ang lee ",9000,30));
list.add(new Person(" � tiger ",2500,22));
list.add(new Person(" Ender � ",3500,21));
Person[] personAry = new Person[list.size()];
personAry = list.toArray(new Person[list.size()]);
System.out.println("--- Before ordering ------------------------------------");
for(Person p : personAry) {
System.out.println(p);
}
Arrays.sort(personAry,new SalaryComparator());
System.out.println("--- Sort by salary ------------------------------------");
for(Person p : personAry) {
System.out.println(p);
}
Arrays.sort(personAry,new AgeComparator());
System.out.println("--- Sorted by year ------------------------------------");
for(Person p : personAry) {
System.out.println(p);
}
}
}
Here are the results:
-- before sorting ----
Salary: 3400 years
Salary: 10000 year
Name: ang lee salary: 9000 year
Salary: 2500 years
Salary: 3500 years
-- sorted by salary --
Salary: 2500 years
Salary: 3400 years
Salary: 3500 years
Name: ang lee salary: 9000 year
Salary: 10000 year
-- sorted by year ----
Salary: 3500 years
Salary: 2500 years
Salary: 3400 years
Salary: 10000 year
Name: ang lee salary: 9000 year