Skip to content
JsonArray

Sort

void Sort(const JSON_ARRAY_SORT_MODE mode)
void Sort(const std::string shorthandKey, const JSON_ARRAY_SORT_MODE mode, const std::string shorthandDelimiter = ".")

This will sort your json array.

First up

You can't sort an array containing just array values. You can sort ANY json type except for ARRAY.

If a value can't be compared

If a value cannot be compared because, for instance, it is of the wrong type, it will just be ignored.

You can call Sort() in two ways:


Shallow sort

A shallow sort is for arrays containing direct values. Like this one:

[
    281,
    0,
    12,
    92
]

or this one

[
    "hello",
    "good morning",
    "bonjour",
    "hallo"
]

because it sorts after the direct value.
You would perform a shallow sort like this:

C++:
myArr.Sort(JSON_ARRAY_SORT_MODE::NUM_ASC);

You have to supply a JSON_ARRAY_SORT_MODE to tell the array HOW to sort.
Do you want to sort alphabetically? Numerically? Ascending or descending?




Deep sort

A deep sort is for complex arrays. Like this one:

An array containing forum comments:
[
    {
        "user_info": {
            "username": "Muggenmal",
            "joined": 6221326,
            "followers": 192
        },
        "message": "Another one bites de_dust 2",
        "like_count": 1993
    },
    {
        "user_info": {
            "username": "Bedrull33",
            "joined": 432523521,
            "followers": 0
        },
        "message": "Huh, I've only played pubg a couple of times. What do you mean?",
        "like_count": 23
    },
    {
        "user_info": {
            "username": "icrouch",
            "joined": 123656,
            "followers": 210
        },
        "message": "Oh ok that makes a lot more sense. I didn't know there is a TDM mode.",
        "like_count": 1993
    }
]

Let's say you want to sort this array after the users follower count. Specifically, after jsonArray[n].user_info.followers.
Sounds complicated? Wrong! Super easy :D

C++:
myArr.Sort("user_info.followers", JSON_ARRAY_SORT_MODE::NUM_ASC);