List collection with objects for alphanumeric mixed sorting

Source: Internet
Author: User
Tags uppercase letter

list<pagedata>    varlist =  [{bomcode=10a, mantotal=4}, {bomcode=10b, mantotal=1}, {bomcode=11a, mantotal =1}, {bomcode=11b, mantotal=1}, {bomcode=12a, mantotal=1}, {bomcode=1a, mantotal=9}, {bomcode=2a, mantotal=168}, { BOMCODE=2B, mantotal=57}, {bomcode=3a, mantotal=440}, {bomcode=3b, mantotal=363}, {bomcode=4a, mantotal=317}, {BOMCode =4B, mantotal=612}, {bomcode=5a, mantotal=175}, {bomcode=5b, mantotal=188}, {bomcode=6a, mantotal=99}, {BOMCode=6B, mantotal=132}, {bomcode=7a, mantotal=48}, {bomcode=7b, mantotal=58}, {bomcode=8a, mantotal=22}, {BOMCode=8B, mantotal= Ten}, {bomcode=9a, mantotal=7}, {bomcode=9b, mantotal=3}]

Now to sort the Bomcode inside the collection object, return a sorted varlist:

Collections.sort (Varlist,NewComparator<pagedata>(){                            /** int compare (Pagedata pd1, Pagedata pd2) returns an integer of the base type, * Returns a negative number indicating: PD1 is less than PD2,               * Return 0 means: PD1 and PD2 are equal, * return positive number means: PD1 is greater than PD2. */@Override Public intCompare (Pagedata pd1, Pagedata pd2) {intNumber1; intnumber2; intASCII1 = 0; intASCII2 = 0; String S1= Pd1.getstring ("Bomcode"); String S2= Pd2.getstring ("Bomcode"); //gets the last digit of each number and determines whether it is an uppercase letter (determines whether the ASCII code is between 65 and 90)                if((int) (S1.tochararray () [S1.length ()-1]) >= && (int) (S1.tochararray () [S1.length ()-1]) <= 90) {Number1= Integer.parseint (s1.substring (0, S1.length ()-1)); ASCII1= (int) (S1.tochararray () [S1.length ()-1]); } Else{number1=Integer.parseint (S1); }                if((int) (S2.tochararray () [S2.length ()-1]) >= && (int) (S2.tochararray () [S2.length ()-1]) <= 90) {number2= Integer.parseint (s2.substring (0, S2.length ()-1)); Ascii2= (int) (S2.tochararray () [S2.length ()-1]); } Else{number2=integer.parseint (S2); }                //sort from small to large                if(Number1 >number2) {                    return1; } Else if(Number1 = = number2) {//The numbers are equal to determine if there are letters and the Order of letters.//If all two have letters, the Order of Judgment//Order ASCII from small to large (i.e. from A to Z)                    if(Ascii1 <ascii2) {                        return1; }                }                 return-1;        }          }); returnvarlist;

The last varlist returned are as follows:

[{bomcode=1a, mantotal=9}, {bomcode=2b, mantotal=57}, {bomcode=2a, mantotal=168}, {bomcode=3b, mantotal=363}, {BOMCode =3A, mantotal=440}, {bomcode=4b, mantotal=612}, {bomcode=4a, mantotal=317}, {bomcode=5b, mantotal=188}, {BOMCode=5A, mantotal=175}, {bomcode=6b, mantotal=132}, {bomcode=6a, mantotal=99}, {bomcode=7b, mantotal=58}, {BOMCode=7A, mantotal =48}, {bomcode=8b, mantotal=10}, {bomcode=8a, mantotal=22}, {bomcode=9b, mantotal=3}, {bomcode=9a, mantotal=7}, { BOMCODE=10B, mantotal=1}, {bomcode=10a, mantotal=4}, {bomcode=11b, mantotal=1}, {bomcode=11a, mantotal=1}, {BOMCode= 12A, mantotal=1}]

List collection with objects for alphanumeric mixed 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.