Android-English-Chinese-English string arrays are sorted by the first letter

Source: Internet
Author: User
Tags collator

In Java, we can use arrays. Sort (string []) to sort string arrays conveniently. For example:

String [] arrays = new string [] {"Gyu", "SDF", "ZF", "Datong", "received", "local", "thirds ", "People", "anti-high-speed trains", "Extensive Algebra", "investment", "and country "}; /* set the language environment */comparator <Object> COM = collator. getinstance (Java. util. locale. china); arrays. sort (arrays, com); For (string item: arrays) {system. out. print (item + "");}

The output is as follows: "The gyu sdf zf local people opposed to high-speed rail extensive algebra and received input from national third-class scores "; in Java, the order is sorted by numbers-> English-> Chinese characters. This sorting method can meet some requirements, but in many cases we do not want to arrange it like this! For example, in anroid, the address book and the music playlist. In these cases, we want to arrange the first letters in the same way as the first letters in Chinese pinyin for easy query. Because this sort algorithm is very complex, for example, strings with the same initial character must be compared with the second and third .... However, it would not be so complicated if we applied the sorting that comes with JDK;
My idea is as follows: in Java, sorting is performed by numbers> English> Chinese characters, then we will add the first letter of the pinyin character and a separator "&" before each character string starting with a Chinese character, and then use the sorting function provided by JDK to sort it, what we get is the array we want to sort. Then traverse the array and remove the string containing the & Symbol & and the first English letter to complete the entire sorting, the specific implementation code is as follows (to obtain Chinese pinyin needs to reference the jar: pinyin4j-2.5.0.jar ):

/*** Sort string numbers by the first letter ** Java Native sorts numbers-> English-> Chinese * In order to sort the English and Chinese Initials together * first replace the first character of a string with the first character of the Chinese character with the first letter of the Chinese character. * In order to distinguish between the strings, add a separator & * and then use the Java Native Sorting Algorithm * to add & remove the first letter to achieve sorting **/public static void main (string [] ARGs) {string [] arrays = new string [] {"Gyu", "SDF", "ZF", "Datong", "received", "local", "thirds ", "man", "anti-high-speed rail", "Extensive Algebra", "investment on", "and country"}; For (INT I = 0; I <arrays. length; I ++) {string STR = arrays [I]; If (Str. length () = 0) return; string alphabet = Str. substring (0, 1);/* determines whether the first character is Chinese. If it is Chinese, add the first letter and symbol of the first character pinyin to the front of the string */If (alphabet. matches ("[\ u4e00-\ u9fa5] +") {STR = getalphabet (STR) + "&" + STR; arrays [I] = STR ;}} /* set the sorting language environment */comparator <Object> COM = collator. getinstance (Java. util. locale. china); arrays. sort (arrays, com);/* traverses the array and removes the identifier & first letter */For (INT I = 0; I <arrays. length; I ++) {string STR = arrays [I]; If (Str. contains ("&") & Str. indexof ("&") = 1) {arrays [I] = Str. split ("&") [1];} system. out. println (arrays [I]) ;}} public static string getalphabet (string Str) {hanyupinyinoutputformat defaultformat = new hanyupinyininoutputformat (); // output all lowercase defaultformats. setcasetype (hanyupinyincasetype. lowercase); // ultformat without tone. settonetype (hanyupinyintonetype. without_tone); string pinyin = NULL; try {pinyin = (string) pinyinhelper. tohanyupinyinstringarray (Str. charat (0), defaultformat) [0];} catch (badhanyupinyinoutputformatcombination e) {e. printstacktrace ();} return pinyin. substring (0, 1 );}

At this time, the output result is: "The Datong people are opposed to high-speed rail extensive algebra Gyu and the National third-class input receives the sdf zf". You can also try to write the Sorting Algorithm by yourself to implement it, it is also not harmful to exercise your thinking.

Related Article

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.