Hybrid sorting of Chinese characters and numbers

Source: Internet
Author: User
Tags collator

Labels: Java, Chinese, digital, hybrid sorting, Chinese, digital, hybrid sorting, Java, Chinese, and digital hybrid sorting algorithms

Recently, we need to implement the hybrid sorting of Chinese characters and numbers. However, we have been searching for it online for a long time and have not found a proper one. For example, you need to sort the following data: "Test 1, test 20, test 10, Test 2 ". Sort by algorithms written by others on the network. The final result is: Test 1, test 10, Test 2, and test 20. However, this result is not what I want. What I need is a hybrid sorting of Chinese characters and numbers. Chinese characters are sorted by the first letter, and numbers are also sorted in ascending order. Therefore, the expected results should be: Test 1, Test 2, test 10, and test 20.

Since there is no suitable algorithm on the network, you have to write it yourself. In the end, we can only find a more complex method, but we can still implement the hybrid sorting of Chinese characters and numbers. The method is summarized as follows:

I. general idea: Convert the numbers in the string to numbers of the specified length, and then sort the data.

Ii. Code:

1. hanzicomparator class. Sort the processed strings by the first letter of the Chinese characters.

/*** Sort by the first letter of a Chinese character */public class hanzicomparator implements comparator <userinfo> {private collator CMP = collator. getinstance (Java. util. locale. china); @ overridepublic int compare (userinfo O1, userinfo O2) {string username1 = o1.gettempusername (); string username2 = o2.gettempusername (); If (null = username1) {If (null = username2) {return 0;} else {return 1 ;}} else if (null = username2) {return-1 ;} else {int result = CMP. compare (username1, username2); return result ;}}}

2. Entity classes to be sorted by userinfo.

package com.test.hanzicomparator;public class UserInfo {private String userName;private String tempUserName;public String getTempUserName() {return tempUserName;}public void setTempUserName(String tempUserName) {this.tempUserName = tempUserName;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}@Overridepublic String toString() {return "UserInfo [userName=" + userName + ", tempUserName="+ tempUserName + "]";}}

3. sortutil class, which focuses on this class. changeinttospecifylength can convert the numbers in the input string into numbers of the specified length, and finally return the complete string after conversion.

Package COM. test. hanzicomparator; import Java. text. decimalformat; import Java. util. arraylist; import Java. util. list; import Java. util. regEx. matcher; import Java. util. regEx. pattern; public class sortutil {/*** convert the number in the string to a specified length numeric string ** @ Param content * @ return returns the converted string */public static string changeinttospecifylength (string content) {list <string> intmatchlist = new arraylist <string> (); // listlist of matched numbers <s Tring> unintsublist = new arraylist <string> (); // The listlist of truncated non-numbers <string> intchangelist = new arraylist <string> (); // list of matched numbers after conversion // expression function: verification must be a number (integer or decimal) string Pattern = "[0-9] + ([0-9] + )? "; // Sum up the usage of (): process the expression in () as a whole, and its overall structure must be satisfied. // (. [0-9] + )?: Indicates that the entire occurrence of () occurs once or once, and does not show pattern P = pattern. compile (pattern); matcher M = P. matcher (content); int unintsubstartindex = 0; // The start index of the non-count Subpart. The default value is 0 while (M. find () {intmatchlist. add (M. group (); int unintsubendindex = 0; // The end index of this non-numeric truncation (the value is the start of the number matching), the default value is 0if (M. start ()! = 0) {unintsubendindex = m. start () ;}// capture a non-numeric string sub = content. substring (unintsubstartindex, unintsubendindex); unintsublist. add (sub); // The ending index of this number match is the start of the next unintsubstartindex = m. end () ;}// convert the matched number to a number of the specified length if (intmatchlist! = NULL &&! Intmatchlist. isempty () {system. out. println (intmatchlist. tostring (); For (string: intmatchlist) {string changestr = changeintlength (string, 9); intchangelist. add (changestr) ;}} system. out. println (unintsublist. tostring (); system. out. println (intchangelist. tostring (); // concatenate data into string temp = ""; if (unintsublist! = NULL & unintsublist! = NULL & unintsublist. size () = intchangelist. size () {for (INT I = 0; I <intmatchlist. size (); I ++) {// non-numeric + converted numeric temp + = unintsublist. get (I) + intchangelist. get (I) ;}content = temp;} return content ;} /*** convert a number to a number of the specified length ** @ Param value * @ Param retlength * @ return returns the numeric string of the specified length */Private Static string changeintlength (string value, int retlength) {string ret = value; If (value! = NULL &&! Value. equals ("") {int intvalue = integer. valueof (value); char [] cc = new char [retlength]; int I = 0; for (I = 0; I <retlength; I ++) {CC [I] = '0';} decimalformat df = new decimalformat (new string (CC); ret = DF. format (intvalue);} return ret;} // public static void main (string [] ARGs) {// string test = "10, health 1.010 test 20 China 111 already 1 "; // system. out. println (TEST); // system. out. println (changeinttospecifylength (TEST ));//}}

4. Main class

Package COM. test. hanzicomparator; import Java. util. arraylist; import Java. util. collections; import Java. util. list; public class main {public static void main (string [] ARGs) {list <userinfo> List = new arraylist <userinfo> (); userinfo info1 = new userinfo (); info1.setusername ("Test 1"); userinfo info2 = new userinfo (); info2.setusername ("test 10"); userinfo info3 = new userinfo (); info3.setusername ("Test 2 "); userinfo in Fo4 = new userinfo (); info4.setusername ("tested 20"); userinfo info5 = new userinfo (); info5.setusername ("tested 201"); list. add (info1); list. add (info2); list. add (info3); list. add (info4); list. add (info5); // first convert the array in the string to the specified length ?? Number for (userinfo: List) {string tempname = sortutil. changeinttospecifylength (userinfo. getUserName (); userinfo. settempusername (tempname);} // sort the converted data ?? Hanzicomparator = new hanzicomparator (); collections. Sort (list, hanzicomparator); system. Out. println ("Result: \ n" + list. tostring ());}}

5. Final output result:

Result:
[Userinfo [username = test 1, tempusername = test 000000001], userinfo [username = test 2, tempusername = test 000000002], userinfo [username = test 10, tempusername = test 000000010], userinfo [username = test 20, tempusername = test 000000020], userinfo [username = test 201, tempusername = test 000000201]


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.