This question already has an answer here:
How is an array sorted by multiple criteria in Swift? For example, an array of dictionaries as shown:
items = [
[
"item":"itemA"
"status":"0"
"category":"B"
],[
"item":"itemB"
"status":"1"
"category":"C"
],[
"item":"itemC"
"status":"0"
"category":"A"
],[
"item":"itemD"
"status":"2"
"category":"A"
]
]
This needs to be sorted as follows:
- category ASC
- status DESC
I have successfully sorted this array based on either condition 1 OR 2, but not both. Below is the code for that:
itemArray.sort({
$0["category"] < $1["category"])
})
How can this be expanded to include multiple sort criteria in a given order?
You want a lexicographic comparison i.e. if the first fields are equal, compare the second fields, otherwise compare the first fields.
The dictionary complicates it a bit since you don’t want to fetch the data out twice and keys may be missing, but that’s actually fine since
==
and<
can handle optional comparisons.There’s actually a
lexicographicCompare
that you could use under some circumstances to do the comparison – but it won’t work in this case because a) optionals, while they can be compared with<
, don’t conform toComparable
so you’d have to check for nils manually, and b) you want to sort the second entry in descending order.