Sorting a basic array¶
First up:
What is a basic array? An array like this, consisting only of either booleans, integers, floats or strings.
We have this json:¶
[
"avocados",
"red beans",
"hazelnuts",
"flour",
"onion powder"
]
Now, how do i sort this darn thing?¶
Quite easily.
Have a look at JSON_ARRAY_SORT_MODE. This enum houses the four sorting modes.
Which are:
- Numeric, Ascending order
- Numeric, Descending order
- Alphabetically, Ascending order
- Alphabetically, Descending order
Because our array has string-types in this case, we will sort alphabetically, in ascending order:
C++:¶
// Let's first create the array
JsonArray myArr;
myArr.Add("avocados");
myArr.Add("redbeans");
myArr.Add("hazelnuts");
myArr.Add("flour");
myArr.Add("onion powder");
// Now sort it!
myArr.Sort(JSON_ARRAY_SORT_MODE::ALPH_ASC);
That's it!
If two values cannot be compared
If two values cannot be compared because, for example, they are of the wrong type, they will just be ignored by the sorter.
That will most likely pile them up at either the start or the end of the array.