Android sorts Chinese character strings by pinyin. Java

Source: Internet
Author: User

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

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.