Java Chinese and English digital sorting

Source: Internet
Author: User

Online to find an example of their own modified under sharing, you can pass multiple sort field array

 Public classSortlist<e>{             Public StaticLogger Loger = Loggerfactory.getlogger (sortlist.class); /*** * Method Description: Custom sort (here temporarily only supports 3 kinds of attributes at the same time, and later to add properties to expand the following type of sort if else) * Here is the first sort according to the Chinese and English first letter *@param1 List of object collections to sort *@param2 Methods Object properties to sort *@param3 The Sorts property is descending or ascending if NULL is passed in the default ascending order (the sorts array is a corresponding to methods) * @Title: Sort * * @modifi ER*/@SuppressWarnings ("All")         Public voidSort (list<e> List,FinalString[] Methods,Finalstring[] sorts) {Collections.sort (list,NewComparator () { Public intCompare (Object A, object B) {intRET = 0; Try{                          if(methods==NULL|| Methods.length==0) {Loger.error ("No sort field passed in"); }Else{Method M1= ((E) a). GetClass (). GetMethod (Methods[0],NULL); Method m2= ((E) b). GetClass (). GetMethod (Methods[0],NULL); if(Sorts = =NULL|| sorts.length==0 | | "ASC". Equals (Sorts[0]) {ret= Chinainitial.getpyindexstr (M1.invoke (((E) a),NULL). ToString (),false). substring (0, 1). CompareTo (Chinainitial.getpyindexstr (M2.invoke ((E) b),NULL). ToString (),false). substring (0, 1)); }Else{ret= Chinainitial.getpyindexstr (M2.invoke (((E) b),NULL). ToString (),false). substring (0, 1). CompareTo (Chinainitial.getpyindexstr (M1.invoke ((E) a),NULL). ToString (),false). substring (0, 1)); }                            if(methods.length>1){                                 for(inti = 0;i<methods.length;i++){                                    if(i==0)Continue; String Method=Methods[i]; Method M3= ((E) a). GetClass (). GetMethod (Method,NULL); Method M4= ((E) b). GetClass (). GetMethod (Method,NULL); Object M3obj= M3.invoke (((E) a),NULL); Object M4obj= M4.invoke (((E) b),NULL); if(M3objinstanceofDate) {                                        if(ret==0){                                          if(sorts==NULL|| sorts.length==0 | | Sorts[i].equals ("ASC") ) {ret=(date) m4obj). CompareTo ((date) m3obj); }Else{ret=(date) m3obj). CompareTo ((date) m4obj); }                                        }                                    }Else if(M3objinstanceofInteger) {                                        if(ret==0){                                            if(sorts==NULL|| sorts.length==0 | | Sorts[i].equals ("ASC") ) {ret=((integer) m4obj). CompareTo ((integer) m3obj); }Else{ret=((integer) m3obj). CompareTo ((integer) m4obj);                            }                                        }                                    }                                } }                        }                    }Catch(Exception e) {loger.error (exception, E); }                    returnret;          }               }); }      }  


Here is the class to get the Chinese initials

/*** * * get Chinese initials * *@authorlxm_09*/ Public classchinainitial {/**     *      * @paramargs*/     Public Static voidMain (string[] args) {String str= "I am Chinese"; System.out.println ("Chinese First Letter:" + getpyindexstr (str,false)); }    /*** * return the first letter * *@paramStrchinese * *@parambupcase * *@return     */     Public StaticString Getpyindexstr (String Strchinese,Booleanbupcase) {        Try{StringBuffer buffer=NewStringBuffer (); byteB[] = strchinese.getbytes ("GBK");//Convert Chinese to byte array             for(inti = 0; i < b.length; i++) {                if((B[i] & 255) > 128) {                    intCHAR1 = b[i++] & 255; Char1<<= 8;//the left shift operator is denoted by "<<", which is the object to the left of the operator, the number of bits specified to the right of the operator to the left, and 0 in the low. In fact, moving the N-bit to the left is equivalent to multiplying by 2 of the n-th square                    intChart = Char1 + (B[i] & 255); Buffer.append (Getpyindexchar (Char) (chart, bupcase)); Continue; }                Charc = (Char) B[i]; if(! Character.isjavaidentifierpart (c))//determines whether the specified character can be a Java//the part of the identifier other than the first character. C= ' A ';            Buffer.append (c); }            returnbuffer.tostring (); } Catch(Exception e) {System.out.println (NewStringBuilder ()). Append ("\u53d6\u4e2d\u6587\u62fc\u97f3\u6709\u9519"). Append (E.getmessage ()). ToString ()); }        return NULL; }    /*** * Get the first letter * *@paramStrchinese * *@parambupcase * *@return     */    Private Static CharGetpyindexchar (CharStrchinese,Booleanbupcase) {        intCHARGBK =Strchinese; Charresult; if(CHARGBK >= 45217 && chargbk <= 45252) Result= ' A '; Else        if(CHARGBK >= 45253 && chargbk <= 45760) Result= ' B '; Else        if(CHARGBK >= 45761 && chargbk <= 46317) Result= ' C '; Else        if(CHARGBK >= 46318 && chargbk <= 46825) Result= ' D '; Else        if(CHARGBK >= 46826 && chargbk <= 47009) Result= ' E '; Else        if(CHARGBK >= 47010 && chargbk <= 47296) Result= ' F '; Else        if(CHARGBK >= 47297 && chargbk <= 47613) Result= ' G '; Else        if(CHARGBK >= 47614 && chargbk <= 48118) Result= ' H '; Else        if(CHARGBK >= 48119 && chargbk <= 49061) Result= ' J '; Else        if(CHARGBK >= 49062 && chargbk <= 49323) Result= ' K '; Else        if(CHARGBK >= 49324 && chargbk <= 49895) Result= ' L '; Else        if(CHARGBK >= 49896 && chargbk <= 50370) Result= ' M '; Else        if(CHARGBK >= 50371 && chargbk <= 50613) Result= ' N '; Else        if(CHARGBK >= 50614 && chargbk <= 50621) Result= ' O '; Else        if(CHARGBK >= 50622 && chargbk <= 50905) Result= ' P '; Else        if(CHARGBK >= 50906 && chargbk <= 51386) Result= ' Q '; Else        if(CHARGBK >= 51387 && chargbk <= 51445) Result= ' R '; Else        if(CHARGBK >= 51446 && chargbk <= 52217) Result= ' S '; Else        if(CHARGBK >= 52218 && chargbk <= 52697) Result= ' T '; Else        if(CHARGBK >= 52698 && chargbk <= 52979) Result= ' W '; Else        if(CHARGBK >= 52980 && chargbk <= 53688) Result= ' X '; Else        if(CHARGBK >= 53689 && chargbk <= 54480) Result= ' Y '; Else        if(CHARGBK >= 54481 && chargbk <= 55289) Result= ' Z '; Elseresult= (Char) (65 + (NewRandom ()). Nextint (25)); if(!bupcase) Result=character.tolowercase (Result); returnresult; }}

Invoke Example

New sortlist<object>== {sortrule}; Sortlist.sort (resultlist, param, sorts);

  

Java Chinese and English digital sorting

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.