Java Chinese sorting

Source: Internet
Author: User
Tags collator

Note: most of the text in this article comes from www.javatang.com. Author: jet MAH
Article: http://www.javatang.com/archives/2007/03/05/5722110.html

The so-called Chinese sorting means sorting by Chinese pinyin, which is not required in Java.
Write algorithms by yourself. The sort method provided in the Java. util. arrays class can directly implement this function.
Example:
Package mypck;
Import java. Text. collator;
Import java. util. arrays;
Import java. util. comparator;
Import java. util. locale;
Public class demo {
Public static void main (string ARGs []) {
String [] STR = {"Zhou Hongbin", "Liao fubin", "Yan Teng", "Peng Chong", "Zhao Yang new", "Wang Xiong", "Wei kaiyong ", "Wang Wenjie "};
// Comparator is a comparator.
Java. util. comparator cptr = collator. getinstance (locale. China );
System. Out. println ("sorted before :");
For (INT I = 0; I <Str. length; I ++)
System. Out. Print (STR + "");
System. Out. println ();
// Sort the array of specified objects according to the order generated by the specified Comparator
Arrays. Sort (STR, cptr );
System. Out. println ("sorted after :");
For (INT I = 0; I <Str. length; I ++)
System. Out. Print (STR + "");
}
}
The running result is as follows:
Sorted before:
Zhou Hongbin Liao Fu bin Peng Chong Zhao Yang new Wang Xiong Wei kaiyong Wang Wenjie
Sorted after:
Li Teng Liao Fu bin Peng Chong Wang Wenjie Wang Xiong Wei kaiyong Zhao Yang Xin Zhou Hongbin
Is to implement the expected function.
PS:
These two days I have been looking at Java internationalization and Eclipse plug-in internationalization. I am very familiar with Java. util. locale.
Therefore, this program is easy to read.
Other knowledge points: comparable, comparator, and collator (including natural language text building search and sorting functions)
To sort Java objects, You can implement the compareble or comparator interfaces. Of course, you can also implement both interfaces.
Collator implements the preceding two interfaces.

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.