I'm trying to sort an array of custom struct in swift 4.2. I want to sort it out in such a way that
- Objects with true
isSelectedbool property are always at top and all other objects should be sort by ascending using a property calledsortOrder(Int). - Objects with
isSelectedproperty should also be sorted bysortOrder(ascending way). So far I was able to achieve the 1st goal but having some problem with the 2nd.
Here's my code:
myArray.sort { (item1, item2) -> Bool in
if item1.isSelected ?? false && item2.isSelected == false {
return true
} else if item2.isSelected ?? false {
return false
}
return item1. sortOrder < item2. sortOrder
}
Please help with the 2nd objective. Thank you.