Java Chinese sort

Source: Internet
Author: User
Tags collator

Sort Chinese names, don't say more, on the code

Java code
  1. Package test;
  2.   
  3. /** 
  4. * @Title: Person.java
  5. * @Copyright: Copyright (c) 2012-11-19
  6. * @Author: Zhangzhao
  7. * @Version 0.1
  8.  */  
  9. public class person{
  10. Private int ID;
  11. Private String name;
  12.       
  13. Public String getName () {
  14. return name;
  15.     }  
  16.   
  17. public void setName (String name) {
  18. This . Name = name;
  19.     }  
  20.   
  21. public int getId () {
  22. return ID;
  23.     }  
  24.   
  25. public void setId (int ID) {
  26. This . id = ID;
  27.     }  
  28.   
  29. }  

Java code

  1. Package test;
  2.   
  3. import java.text.Collator;
  4. import java.util.Comparator;
  5.   
  6. /** 
  7. * @Title: Sortchinesename.java
  8. * @Description: Sort Chinese characters
  9. * @Function: Sort Chinese characters
  10. * @Copyright: Copyright (c) 2012-11-19
  11. * @Author: Zhangzhao
  12. * @Version 0.1
  13.  */  
  14. public class sortchinesename implements Comparator<person >{
  15. Collator cmp = collator.getinstance (Java.util.Locale.CHINA);
  16. @Override
  17. public int Compare (person O1, person O2) {
  18. if (Cmp.compare (O1.getname (), O2.getname ()) >0) {
  19. return 1;
  20. }Else if (Cmp.compare (O1.getname (), O2.getname ()) <0) {
  21. return -1;
  22.         }  
  23. return 0;
  24.     }  
  25. }  

Java code

  1. Package test;
  2.   
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.   
  7. public class testbean {
  8.   
  9. public static void main (String args[]) {
  10. list<person> List = new arraylist<person> ();
  11. Person p = new person ();
  12. P.setid (1);
  13. P.setname ("Zhang San");
  14. List.add (P);
  15. p = new person ();
  16. P.setid (2);
  17. P.setname ("John Doe");
  18. List.add (P);
  19. p = new person ();
  20. P.setid (3);
  21. P.setname ("Harry");
  22. List.add (P);
  23. p = new person ();
  24. P.setid (4);
  25. p.setname ("Zhao Liu");
  26. List.add (P);
  27. //positive order
  28. collections.sort (list, new sortchinesename ());
  29. System.out.println ("Chinese name is ordered in sequence:");
  30. For (person pp:list) {
  31. System.out.println (Pp.getid () +","+pp.getname ());
  32.         }  
  33. System.out.println ("---------------Magical dividing line--------------------");
  34. //reverse
  35. collections.reverse (list);
  36. System.out.println ("Chinese names in reverse order:");
  37. For (person pp:list) {
  38. System.out.println (Pp.getid () +","+pp.getname ());
  39.         }  
  40. System.out.println ("---------------Magical dividing line--------------------");
  41. Collections.sort (List,collections.reverseorder (new sortchinesename ()));
  42. System.out.println ("Chinese names in reverse order:");
  43. For (person pp:list) {
  44. System.out.println (Pp.getid () +","+pp.getname ());
  45.         }  
  46.     }  
  47. }  

Printing results:

Chinese names are arranged in a positive order:

2, John Doe

3, Harry

1, Zhang San

4, Zhao Liu

---------------Magical Split-line--------------------

Chinese names in reverse order:

4, Zhao Liu

1, Zhang San

3, Harry

2, John Doe

---------------Magical Split-line--------------------

Chinese names in reverse order:

4, Zhao Liu

1, Zhang San

3, Harry

2, John Doe

Java Chinese sort

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.