Android: ordena JSONArray por atributo de objeto

I'm trying to create a list of "events" for an agenda. To do this, I recieve a JSON response which contains several month objects, which in turn contain an array of event-objects.

When I receive my response, all months are ordered, as shown below:

    {
  "Agenda/Future": {
    "November 2011/0": [
      {
        "id": "5710",
        "eventid": "",
        "name": "test7",
        "startdate": "nov 9",
        "datecompute": "2011-11-09T12:00:00",
        "group_month": "November 2011",
        "flg_Data": "Database"
      },
      {
        "id": "5707",
        "eventid": "",
        "name": "test4",
        "startdate": "nov 17",
        "datecompute": "2011-11-17T12:00:00",
        "group_month": "November 2011",
        "flg_Data": "Database"
      },
      {
        "id": "5704",
        "eventid": "",
        "name": "test",
        "startdate": "nov 30",
        "datecompute": "2011-11-30T12:00:00",
        "group_month": "November 2011",
        "flg_Data": "Database"
      }
    ],
    "December 2011/1": [
      {
        "id": "5705",
        "eventid": "",
        "name": "test2",
        "startdate": "dec 28",
        "datecompute": "2011-12-28T12:00:00",
        "group_month": "December 2011",
        "flg_Data": "Database"
      }
    ],
    "November 2013/2": [
      {
        "id": "5706",
        "eventid": "",
        "name": "test3",
        "startdate": "nov 1",
        "datecompute": "2013-11-01T12:00:00",
        "group_month": "November 2013",
        "flg_Data": "Database"
      },
      {
        "id": "5708",
        "eventid": "",
        "name": "test5",
        "startdate": "nov 15",
        "datecompute": "2013-11-15T12:00:00",
        "group_month": "November 2013",
        "flg_Data": "Database"
      },
      {
        "id": "5709",
        "eventid": "",
        "name": "test6",
        "startdate": "nov 15",
        "datecompute": "2013-11-15T12:00:00",
        "group_month": "November 2013",
        "flg_Data": "Database"
      }
    ]
  }
}

First there is November 2011, then December 2011, and finally November 2013.

However after parsing this response, the order has changed to November 2011, November 2013, December 2011. Like so:

enter image description here

Obviously, this can cause quite some confusion.

My theory is that due to the use of JSONArray eventNames = agenda.names();, which gets the names of the month objects, the order changes to one based on the actual month, not the year. This has been confirmed after adding another event in March 2022, and this event ends up first. I checked through my code, and nowhere can I find any sort of function that would change the ordering, making the names(); the most probably culprit.

My question is, how can I make sure the objects in my initial JSONArray get ordered by the month + year, rather then just the month. Removing the names() method is not an option, considering the fact that the objectnames for the months are dynamic (based on month+year). I also would rather not change to a different parsing system (GSON, Jackson), since this is the only one that has been succesfull at parsing my reponse (see previous asked questions).


Eventually managed to fix it by looping over an array containing the full names, and then checking whether that name contained any of the parts from the ordered list (the one containing the years + id). If the string contains that part, add it to a new array on the position of the year+id it contained. Code can be found below

String[] test = new String[eventNames.length()];
                    String[] test2;

                    for (int x = 0; x < eventNames.length(); x++){
                        //System.out.println(eventNames.get(x).toString());
                        String[] xx = eventNames.get(x).toString().split(" ");
                        test[x] = xx[1];


                    }

                    Arrays.sort(test);

                    String[] test3 = new String[eventNames.length()];
                    for(int q = 0; q < eventNames.length(); q++){
                        String str = eventNames.get(q).toString();
                        for(int p = 0; p < test.length; p++){
                            if(str.contains(test[p])){
                                test3[p] = str;
                            }
                        }
                    }


                    System.out.println(test.toString());
                    for (int x = 0; x < eventNames.length(); x++){
                        System.out.println("maand :"+x+" "+test3[x]);   
                    }

preguntado el 08 de noviembre de 11 a las 12:11

1 Respuestas

I woudl speculate that JSON Object is backed with map, and names does merely Map.keys() - order of keys is undefined. At leas API spec says nothing about any sorting / ordering of individual fields - so you will have to order those entries yourself - defining comparator based on split(" ") is not that difficult

respondido 08 nov., 11:17

This sounds like a logic idea, but I'm not quite sure how I would do this. I managed to split the month + year + id into just the month, and just the year+id, and sort that. That seems to work, but how would I connect the 2 pieces together again? My idea was using 2 String arrays, and comparing the items in the list for years+id with the full strings in the other array, but apparently you can't check whether a string in an array contains a piece of a string. - Sander van't Veer

After you sorted the keys, you can just retrieve agenda entries by name. - Konstantin Pribluda

Not sure if that works when you only know part of the name, but I did manage to fix it eventually, posting the answer in my initial post - Sander van't Veer

Proper way is to implement comparator ( java.util.Comparator ) , move elements into List and then sort it - then you have just a lost of sorted names - Konstantin Pribluda

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.