Java stringarray sorting and Chinese sorting

Source: Internet
Author: User
Tags collator

1. String Array sorting:

String [] reallist =...

// Sort by alphabet
Alphabetcompare AC = new alphabetcompare ();
Arrays. Sort (reallist, AC );

Public class alphabetcompare implements comparator <string> {

@ Override
Public int compare (string str1, string str2 ){
Return str1.compareto (str2 );
}
}

Very simple !!

2. In order to sort the obtained names in alphabetical order, write the following class:

JavaCode

  1. Public ClassAlphabetcomparatorImplementsComparator <string> {
  2. PrivateRulebasedcollator collator;
  3. PublicAlphabetcomparator (){
  4. Collator = (rulebasedcollator) collator
  5. . Getinstance (Java. util. locale. China );
  6. }
  7. @ Override
  8. Public IntCompare (string obj1, string obj2 ){
  9. Collationkey C1 = collator. getcollationkey (obj1 );
  10. Collationkey C2 = collator. getcollationkey (obj2 );
  11. ReturnCollator. Compare (collationkey) C1). getsourcestring (),
  12. (Collationkey) C2). getsourcestring ());
  13. }
  14. }

This is in ascending order. Changing C1 and C2 is in descending order.

Call method Java code

    1. Alphabetcomparator CFC =NewAlphabetcomparator ();
    2. Collections. Sort ([data to be sorted], CFC)

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.