Niels Ferguson
Niels Ferguson

Reputation: 372

Java - Group and Sort Array of Objects by property

I have got an array of objects of the form:

[
    {
        "id": "id_1",
        "type": "A",
        "value": "value_1",
        "label": "label_1"
    },
    {
        "id": "id_2",
        "type": "B",
        "value": "value_2",
        "label": "label_2"
    },
    {
        "id": "id_3",
        "type": "C",
        "value": "value_3",
        "label": "label_3"
    },
    {
        "id": "id_4",
        "type": "A",
        "value": "value_4",
        "label": "label_4"
    }
]

This needs to become an array of arrays. Where The arrays are grouped by type. And the id field can be omitted.

So I should end up with:

[
    [
        {
            "value": "value_1",
            "label": "label_1"
        },
        {
            "value": "value_4",
            "label": "label_4"
        }
    ],
    [
        {
            "value": "value_2",
            "label": "label_2"
        }
    ],
    [
        {
            "value": "value_3",
            "label": "label_3"
        }
    ]
]

I want to avoid using loops if possible. Any help would be greatly appriciated!

Upvotes: 0

Views: 901

Answers (1)

GameDroids
GameDroids

Reputation: 5662

I would try it like this:

Arrays.sort(originalArray, (o1, o2) -> o1.getType().compareTo(o2.getType()));

which would simply result in an "ordered by type". If you wish you can also sort the "sub arrays" like this:

Arrays.sort(originalArray, (o1, o2) -> {
     int compare = o1.getType().compareTo(o2.getType());
     if(compare == 0){
         return o1.getValue().compareTo(o2.getValue());
     }else{
         return compare;
     }
});

which will sort by value if the types of two elements are equal. Now it is much much simpler if you where to use Lists to process the elements and if needed you can convert the lists back to arrays.

ArrayList<ArrayList> result = new ArrayList<ArrayList>(); // this is going to be your result list (untyped in this example)
ArrayList newList = new ArrayList();  // first you create the very first sub list
newList.add(originalList.get(0));     // add the first element to the first sublist
result.add(newList); // then you add the first list to your result list 

int i=0;             // this is going to be the index for the "result list"

for(MyObject element : originalList){
    // you check if the next element is part of the last list you added 
    if(result.get(i).get(result.get(i).size()-1).getType()==element.getType()){   // if the last element of your last list is of the same type as the current element you are checking
        result.get(i).add(element);    // then you just add the element to the last list
    }else{
        ArrayList newList = new ArrayList();  // otherwise you create a new list
        newList.add(element);                 // add the element to it
        result.add(newList);                  // and you add the new list to your result list
        i++;  // since you have now a new sub list you increment the index
    }
}

Sorry if it looks messy - there are still a lot of things to enhance, I just thought it would help you to see one possible implementation (with one simple loop)

Upvotes: 1

Related Questions