简体   繁体   中英

Sorting the list of JSONObjects on the basis of particular key

I need to sort list of JSONObjects on the basis of "id" key. I am sorting using collections.sort and a comparator. My ids are as follows that I need to sort -

9721df798198##-1-2

9721df798198##-1-2-4

9721df798198##-1-2-4-9-14-16

9721df798198##-1-2-4-9-14-16-26

9721df798198##-1-2-4-9-14-16-26-27

9721df798198##-1-2-4-9-14-16-112

For sorting these id I had override compare method. Please refer my code below -

Collections.sort( jsonObjList, new Comparator<JSONObject>() {

        private static final String KEY_ID = "id";

        public int compare(JSONObject o1, JSONObject o2) {
            String str1 = new String();
            String str2 = new String();

            str1 = (String) o1.get(KEY_ID);
            str2 = (String) o2.get(KEY_ID);

            return compareString(str1, str2);   
        }

        public int compareString(String str1, String str2){

            String subString = str1.substring(str1.indexOf("##")+2, str1.length());
            String subString1 = str2.substring(str2.indexOf("##")+2, str2.length());
            subString = subString.replace("-","");
            subString1 = subString1.replace("-","");


            return new BigInteger(subString).compareTo(new BigInteger(subString1));

        }
    });

I used a substring to match two strings as before "##" the id will be same only it will change after "##".The sorted result that I get using this piece of code is -

9721df798198##-1
9721df798198##-1-2
9721df798198##-1-2-4
9721df798198##-1-2-4-9
9721df798198##-1-2-4-9-14-16
9721df798198##-1-2-4-9-14-16-26
9721df798198##-1-2-4-9-14-16-112
9721df798198##-1-2-4-9-14-16-26-27
9721df798198##-1-2-4-9-14-16-112-113
9721df798198##-1-2-4-9-14-16-26-27-28
9721df798198##-1-2-4-9-14-16-26-27-28-29
9721df798198##-1-2-4-9-14-16-112-113-114

but this is not the correct sorted result, the expected result after sorting should be -

9721df798198##-1
9721df798198##-1-2
9721df798198##-1-2-4
9721df798198##-1-2-4-9
9721df798198##-1-2-4-9-14-16
9721df798198##-1-2-4-9-14-16-26
9721df798198##-1-2-4-9-14-16-26-27
9721df798198##-1-2-4-9-14-16-26-27-28
9721df798198##-1-2-4-9-14-16-26-27-28-29
9721df798198##-1-2-4-9-14-16-112
9721df798198##-1-2-4-9-14-16-112-113
9721df798198##-1-2-4-9-14-16-112-113-114

Please can anybody help me on this, how can I get the expected sorted results, Thanks for help.

Update your compateString method as follows:

u need to compare each step of the id alone.

public  static int compareString(String str1, String str2){

    String subString = str1.substring(str1.indexOf("##")+3, str1.length());
    String subString1 = str2.substring(str2.indexOf("##")+3, str2.length());
    String[] array1 = subString.split("-");
    String[] array2 = subString1.split("-");
    for(int i=0;i< array1.length && i< array2.length;i++) {
         BigInteger b1 = new BigInteger(array1[i]);
         BigInteger b2 = new BigInteger(array2[i]);
        if(b1.compareTo(b2) >0) //b1 is larger than b2
            return 1;
        if(b1.compareTo(b2) <0)
            return -1;

    }
    if(array1.length == array2.length)//both numbers are equal
        return 0;

    if(array1.length > array2.length)
        return 1;
    return -1;

}

I don't recommend using BigInteger to try and get an easy comparison when ids can be any length and quickly make it not economical.

There are essentially three parts to the id.

  1. Alphanumeric sequence ( String compareTo )
  2. List of numbers (Convert to int and compare)
  3. Length of list of numbers (same sequence until short list runs out, longer list is sorted lower)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM