Android realizes Chinese pinyin sorting method _android

Source: Internet
Author: User

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.

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.