Use collator and collections. sort to sort the list in Chinese. Note the differences with arrays. sort.

Source: Internet
Author: User
Tags collator

// Relationship between the two: 1. The internal implementation of coollections. sort is implemented using arrays. sort.

// 2. If the objects in the list to be sorted have implemented the comparable interface, you can use arrays. Sort To sort objects.

// 3. If the objects in the list to be sorted do not implement the comparable interface, use collections. sort to sort the objects and implement an instance of the comparator interface by yourself. Collections. Sort has two refactoring methods.
Import java. Text. collator;
Import java. util. arraylist;
Import java. util. arrays;
Import java. util. collections;
Import java. util. comparator;
Import java. util. List;
Import java. util. locale;

Public class testlocale {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){

List sourcelist = new arraylist ();
Sourcelist. Add ("medium ");
Sourcelist. Add ("Hua ");
Sourcelist. Add ("person ");
Sourcelist. Add ("Citizen ");
Sourcelist. Add ("Total ");
Sourcelist. Add ("and ");
Sourcelist. Add ("country ");
Sourcelist. Add ("stupid ");

// 1. Print the Array
System. Out. println ("first print -------------------");
For (INT I = 0; I <sourcelist. Size (); I ++ ){
System. Out. println (sourcelist. Get (I ));
}

// 2. Sort arrays and print the Array
System. Out. println ("second print -------------------");
Object [] sstrings = sourcelist. toarray ();
Arrays. Sort (sstrings );
For (INT I = 0; I <sstrings. length; I ++ ){
System. Out. println (sstrings [I]);
}

// 3. Sort collections
Sourcelist. Clear ();
Sourcelist. Add ("medium ");
Sourcelist. Add ("Hua ");
Sourcelist. Add ("person ");
Sourcelist. Add ("Citizen ");
Sourcelist. Add ("Total ");
Sourcelist. Add ("and ");
Sourcelist. Add ("country ");
Sourcelist. Add ("stupid ");
Collections. Sort (sourcelist, new comparator (){

Public int compare (object _ O1, object _ O2 ){
Return chinesecompare (_ O1, _ O2 );
}

});
System. Out. println ("the third print -------------------");
For (INT I = 0; I <sourcelist. Size (); I ++ ){
System. Out. println (sourcelist. Get (I ));
}

}

Private Static int chinesecompare (object _ ochinese1, object _ ochinese2 ){
Return collator. getinstance (locale. Chinese). Compare (_ ochinese1,
_ Ochinese2 );
}

}

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.