Java Chinese sort

Source: Internet
Author: User
Tags arrays collator locale sort
In the first case:

    1. Comparator cmp = collator.getinstance (Java.util.Locale.CHINA);
    2. String[] arr = {"John", "Dick", "Harry", "Liu Liu"};
    3. Arrays.sort (arr, CMP);
    4. for (int i = 0; i < arr.length; i++)
    5. System.out.println (Arr[i]);


In the second case:

Comparablebean.java
Import Java.text.CollationKey;
Import Java.text.Collator;
Import Java.text.RuleBasedCollator;
Import Java.util.Comparator;

public class comparablebean{
private String name;

Public Comparablebean (String name) {

THIS.name = name;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}
}
Class Comparablebeancomparator implements Comparator//<comparablebean>
{
RuleBasedCollator collator; can set your rules for the instance "Collator"
Public Comparablebeancomparator ()
{
Collator = (rulebasedcollator) collator.getinstance (Java.util.Locale.CHINA);//Try testing various locales
}
public int Compare (object Obj1, Object obj2) {
String tempname1 = ((Comparablebean) obj1). GetName ();
String tempname2 = ((Comparablebean) obj2). GetName ();

Collationkey C1 = Collator.getcollationkey (TEMPNAME1);
Collationkey C2 = Collator.getcollationkey (tempname2);
Return Collator.compare ((Collationkey) C1). Getsourcestring (),
((Collationkey) c2). getsourcestring ());
Return Collator.compare ((collationkey) c2). Getsourcestring (),
((Collationkey) C1). Getsourcestring ());
}
public int Compare (Comparablebean obj1, Comparablebean obj2) {
String tempname1 = Obj1.getname ();
String tempname2 = Obj2.getname ();
//
Collationkey C1 = Collator.getcollationkey (TEMPNAME1);
Collationkey C2 = Collator.getcollationkey (tempname2);
Return Collator.compare ((Collationkey) C1). Getsourcestring (),
((Collationkey) c2). getsourcestring ());
// }
}
The end of Comparablebean.java

Test code:

Comparablebean[] Namecontent = {New Comparablebean ("Everything starts from the actual"),
New Comparablebean ("Invincible"), new Comparablebean ("polynomial"),
New Comparablebean ("implement"), New Comparablebean ("Close to the masses"),
New Comparablebean ("Cardinal"), New Comparablebean ("gnashing of Teeth"),
New Comparablebean ("respectfully"), new Comparablebean ("civilian police"),
New Comparablebean ("Business Contract Responsibility System")};
Arrays.sort (Namecontent,new comparablebeancomparator ());
for (int i = 0; i < namecontent.length; i++) {
System.out.println (Namecontent[i].getname ());
}

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.