Today, I found someone asking how to sort Chinese character strings in pinyin. I checked the document and found that Java sorted the strings according to the encoding size of each UNICODE character by default. Next I Found Out (what will happen to the world if Google is not available ?) There is a Java. Text. collator class, but some people on the Forum said this class is useless. It has the same effect as the default sorting. You need to write a class in C # To generate a Java program. If so, what should I do with Java?
So I changed the test. Java of others and tried it with the specified locale and without the specified locale respectively. The results are OK:
Import Java. util. *; import Java. text. *; public class Test2 {public static void testdefault () {string [] arr = {"Zhang San", "Li Si", "Wang Wu", "Liu"}; arrays. sort (ARR); For (INT I = 0; I <arr. length; I ++) system. out. println (ARR [I]); system. out. println ();} public static void testchinalocale () {// comparator CMP = (rulebasedcollator) Java. text. collator. getinstance (Java. util. locale. china); // try testing various locales comparator CMP = collator. getinstance (Java. util. locale. china); string [] arr = {"Zhang San", "Li Si", "Wang Wu", "Liu"}; arrays. sort (ARR, CMP); For (INT I = 0; I <arr. length; I ++) system. out. println (ARR [I]); system. out. println ();} public static void testdefalocallocale () {// comparator CMP = (rulebasedcollator) Java. text. collator. getinstance (Java. util. locale. china); // try testing various locales comparator CMP = collator. getinstance (); string [] arr = {"Zhang San", "Li Si", "Wang Wu", "Liu"}; arrays. sort (ARR, CMP); For (INT I = 0; I <arr. length; I ++) system. out. println (ARR [I]); system. out. println ();} public static void main (string ARGs []) {testdefault (); testchinalocale (); testdefalocallocale ();}}
Output result:
Liu
Zhang San
Li Si
Wang Wu
Li Si
Liu
Wang Wu
Zhang San
Li Si
Liu
Wang Wu
Zhang San
From: http://hi.baidu.com/luyanlong1/item/4d80b531994b2194c2cf2911