Java implementation of Chinese characters in alphabetical order (sample code) _java

Source: Internet
Author: User
Tags static class

In the recent project, we need to sort the characters by pinyin.

Copy Code code as follows:

public static void Main (string[] args) {

Comparator cmp = collator.getinstance (Java.util.Locale.CHINA);

String[] arr = {"John", "Dick", "Harry", "Liu Liu", "Zhou Wentao", "Dai Li", "Dai-ah"};
String[] arr1 = {"Life", "culture and education", "politics", "nature", "religion", "trade", "Military Affairs", "Business"};

For simple words, Daya, Dai Li in the same situation in the first word, the second word, very intelligent OH
Arrays.sort (arr, CMP);
for (int i = 0; i < arr.length; i++)
Output: Daya, Dai Li, Dick, Liu Liu, Harry, John, Zhou Wentao
System.out.println (Arr[i]);

For the complexity of the word, the "army" was photographed at the end.
Arrays.sort (ARR1, CMP);
for (int i = 0; i < arr1.length; i++)
Output: Trade, life, culture and education, politics, nature, religion, trading, military affairs
System.out.println (Arr1[i]);
}

About the word in the phonetic sorting, you can use the Util package of the comparator interface, according to the rules of the definition of compare method can be. Here's an example:
Copy Code code as follows:

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;//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 "Name:" + this.name + "T Salary:" + this.salary + "T Age:" + this.age;
}
}

/*
* Sorted 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;
}
}


/*
* Sorted by age, from low to high
*/
Static Class Agecomparator implements Comparator {
public int Compare (object O1, Object O2) {

Integer Age1 = ((person) O1).
Integer Age2 = ((person) O2).
if (Age1-age2 > 0)
return 1;
if (Age1-age2 < 0)
return-1;
Else
return 0;
}
}


The Main method tests:
Copy Code code as follows:

public static void Main (string[] args) {

list<person> list = new arraylist<person> ();
List.add (New person ("Zhang Yang", 3400,25));
List.add (New person ("King serious", 10000,27));
List.add (New Person ("Ang Lee", 9000,30));
List.add (New person ("Chris Lau 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---sort");
for (person P:personary) {
SYSTEM.OUT.PRINTLN (P);
}
Arrays.sort (Personary,new salarycomparator ());
SYSTEM.OUT.PRINTLN ("---------------------------------------by salary");
for (person P:personary) {
SYSTEM.OUT.PRINTLN (P);
}

Arrays.sort (Personary,new agecomparator ());
SYSTEM.OUT.PRINTLN ("---by age------------------------------------");
for (person P:personary) {
SYSTEM.OUT.PRINTLN (P);
}

}
}

The following output results:

---sort before------------------------------------
Name: Zhang Yang Salary: 3400 Age: 25
Name: Wang Serious salary: 10,000 years old: 27
Name: Ang Lee Salary: 9000 Age: 30
Name: Chris Lau Tiger Salary: 2500 Age: 22
Name: Ender's Salary: 3500 Age: 21

---sorted by salary------------------------------
Name: Chris Lau Tiger Salary: 2500 Age: 22
Name: Zhang Yang Salary: 3400 Age: 25
Name: Ender's Salary: 3500 Age: 21
Name: Ang Lee Salary: 9000 Age: 30
Name: Wang Serious salary: 10,000 years old: 27

---sorted by age-------------------------------
Name: Ender's Salary: 3500 Age: 21
Name: Chris Lau Tiger Salary: 2500 Age: 22
Name: Zhang Yang Salary: 3400 Age: 25
Name: Wang Serious salary: 10,000 years old: 27
Name: Ang Lee Salary: 9000 Age: 30

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.