This article illustrates the method of the first-alphabetical ordering of the mixed-English string array in Java programming implementation. Share to everyone for your reference, specific as follows:
In Java, for sorting string arrays, we can use the Arrays.sort (string[]) method to sort easily. For example:
string[] Arrays = new string[] {"Gyu", "SDF", "ZF", "Datong", "received", "Place", "three equal", "people", "against high-speed rail", "Pan Algebra", "on input", "and Country"};
/* Set locale
/comparator<object> com = collator.getinstance (java.util.Locale.CHINA);
Arrays.sort (Arrays, com);
for (String item:arrays) {
System.out.print (item+ "");
}
The result of the output is: "Gyu SDF ZF people place against the high-speed rail Pan algebra and the national tripartite input received"; The order in Java is sorted according to the number-> English-> Chinese characters, which can satisfy some of the requirements, but in many cases We do not want this arrangement! For example, the Address Book in Anroid, music playlist, and so on, in these cases we want the first letter of the English alphabet and the Chinese phonetic Alphabet to arrange together to facilitate the query. Because this sort of sorting algorithm is very complex, such as the first letter of the same string will be compared to the second, third .... But it wouldn't be so complicated if we applied the JDK's own sequencing;
My idea is this: since the sort in Java is sorted by the number-> the English-> Chinese characters, then we put a string preceded by each character with the first letter of Pinyin of the first character of the string and a "&". Then using the sort functions provided by JDK, we get the sort of array we want. And then iterate through the array, will contain the & symbol of the string to remove & and the first English letter to complete the order, the implementation of the Code is as follows (get Chinese pinyin needs to refer to the Jar:pinyin4j-2.5.0.jar):
/** * Sort the number of strings by the first letter * * Java Native sort is digital-> English-> Chinese * in order to arrange the English and Chinese initials together the first character of the string is changed to the first letter of the Chinese character with the string * in order to differentiate between the middle
Add a separator & * Then use the Java native Sort algorithm to remove & and initials from the string containing the & character to achieve the sort purpose */public static void main (string[] args) {
string[] Arrays = new string[] {"Gyu", "SDF", "ZF", "Datong", "received", "Place", "three equal", "people", "against high-speed rail", "Pan Algebra", "on input", "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); /* To determine whether the first character is Chinese, the first letter and & symbol of the first character will be added to the front of the string in Chinese (alphabet.matches ("[\\u4e00-\\u9fa5]+")) {str = Getalphabet (str)
+ "&" + str;
Arrays[i] = str;
/* Set the Sort locale language environment * * comparator<object> com = collator.getinstance (Java.util.Locale.CHINA);
Arrays.sort (Arrays, COM);
/* Traverse Array, remove identifier & and 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 Hanyupinyinoutputformat
();
Output Pinyin all lowercase defaultformat.setcasetype (hanyupinyincasetype.lowercase);
Without tonal defaultformat.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 point the output is: "Datong people in the local opposition to high-speed rail pan-algebra Gyu and the national three equal inputs received SDF ZF", we can also try to write their own sorting algorithm to achieve, exercise the thinking is not bad, hehe.
I hope this article will help you with Java programming.