In Java, the tokens are sorted by pinyin.

Source: Internet
Author: User

Recently, traditional Chinese characters need to be sorted by pinyin.

 
Public static void main (string [] ARGs) {comparator CMP = collator. getinstance (Java. util. locale. china); string [] arr = {"Zhang San", "Li Si", "Wang Wu", "Liu", "Zhou JIU", "Dai Yun ", "Dai a"}; string [] arr1 = {"life", "Culture and Education", "Politics", "nature", "Religion", "economy ", "anecdote", "Economy and Trade"}; // valid for traditional Chinese characters. Dai A and Dai Yi use the same Chinese character as the first character and use the same Chinese alphabet as the second character, very intelligent. sort (ARR, CMP); For (INT I = 0; I <arr. length; I ++) // output: Dai A, Dai Yi, Li Si, Liu, Wang Wu, Zhang San, Zhou weisystem. out. println (ARR [I]); // It is ineffective for traditional Chinese characters, and the traditional "anecdote" is shot at the final arrays. sort (arr1, CMP); For (INT I = 0; I <arr1.length; I ++) // outputs: economy and Trade, life, culture and education, politics, nature, religion, economy, and Administrative Affairs system. out. println (arr1 [I]);}

You can use the comparator interface in the util package to implement the compare method based on custom rules. The following is an example:

 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; // annual salary person (string N, integer S, integer) {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 year:" + this. age ;}}
/** 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 ;}}

Main method:

Public static void main (string [] ARGs) {list <person> List = new arraylist <person> (); list. add (new person ("", 3400,25); list. add (new person ("", comment, 27); list. add (new person ("Li an",); list. add (new person ("tiger",); list. add (new person ("Andre", 3500,21); person [] personary = new person [list. size ()]; personary = List. toarray (new person [list. size ()]); system. out. println ("--- Before sorting ------------------------------------"); 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 ("--- sort by year and then ------------------------------------"); For (person P: personary) {system. out. println (p );}}}

The following are the results of outputs:

--- Before sorting ------------------------------------
Name: min Yang salary: 3400 min: 25
Name: Wang Yuan salary: 10000 years salary: 27
Name: Li an salary: 9000 salary: 30
Name: Xiaohu salary: 2500 salary: 22
Name: Andrew's salary: 3500 salary: 21

--- Sort by salary ------------------------------------
Name: Xiaohu salary: 2500 salary: 22
Name: min Yang salary: 3400 min: 25
Name: Andrew's salary: 3500 salary: 21
Name: Li an salary: 9000 salary: 30
Name: Wang Yuan salary: 10000 years salary: 27

--- ---------------------------------------------- After sorting by year ------------------------------------
Name: Andrew's salary: 3500 salary: 21
Name: Xiaohu salary: 2500 salary: 22
Name: min Yang salary: 3400 min: 25
Name: Wang Yuan salary: 10000 years salary: 27
Name: Li an salary: 9000 salary: 30

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.