This question already has an answer here:
- How do I sort an array of structs by multiple values? 3 answers
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?