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.