This article needs to be a group of data by a certain field of Chinese phonetic sorting, to share to everyone Android to achieve Chinese pinyin sorting method for everyone's reference, the specific content as follows
1, Test testing class:
Pinyincomparator comparator = new Pinyincomparator ();
Collections.sort (strlist, comparator);
Where the data in the strlist, can be any object, but to Pinyincomparator in the corresponding changes in the Compare, I demo for string[].
2, Pinyincomparator sorting class:
public class Pinyincomparator implements comparator<object> {/** * comparison two strings */public int compare (OBJ
ECT O1, Object O2) {string[] name1 = (string[]) O1;
String[] name2 = (string[]) O2;
String str1 = Getpingyin (name1[0]);
String str2 = Getpingyin (name2[0]);
int flag = Str1.compareto (STR2);
return flag; /** * Converts the Chinese in the string to pinyin, the other characters unchanged * * @param inputstring * @return/public string Getpingyin (stri
ng inputstring) {hanyupinyinoutputformat format = new Hanyupinyinoutputformat ();
Format.setcasetype (hanyupinyincasetype.lowercase);
Format.settonetype (Hanyupinyintonetype.without_tone);
Format.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);
char[] input = Inputstring.trim (). ToCharArray ();//convert string to character array string output = ""; try {for (int i = 0; i < input.length i++) {//\\u4E00 is a Unicode encoding that determines whether the Chinese if (Java.lang.Char
Acter.tostring (Input[i]). Matches ( "[\\u4e00-\\u9fa5]+")) {/////() () () () () () () () () () temp = Pinyinhelper.tohanyupinyinstrin
Garray (Input[i], format);
Pinyin First pronunciation output + = temp[0]; }//uppercase letters are converted to lowercase else if (input[i] > ' A ' && input[i] < ' Z ') {output + = Java.la Ng.
Character.tostring (Input[i]);
Output = Output.tolowercase ();
Output + + java.lang.Character.toString (Input[i]);
The catch (Exception e) {log.e ("Exception", e.tostring ());
return output;
}
}
The above is the entire content of this article, I hope to help you learn.