Reputation: 2638
I have objects that are in an array I'd like to sort
let person = Person()
person.isHome = true
person.firstname = "Foo"
person.surname = "Bar"
etc...
I fetch all the people
guard let allPeople = controller.fetchedObjects as? Array<Person> else { return }
Now I'd like to filter and sort
var homeArray = allPeople
.filter { $0.isHome == true }
.sort { $0.surname < $1.surname }
All fine... except, what if I want extra 'sort descriptors' like this...
var homeArray = allPeople
.filter { $0.isHome == true }
.sort { $0.surname < $1.surname }
.sort { $0.firstname < $1.firstname }
.sort { $0.isOut == true && $1.isOut == false }
I still want them ordered by surname, but I also want them sorted by first name after surname and injured above non injured... What is the syntax? I can't seem to find it anywhere... Do I have to go back to using NSArray?
Is it as simple as
.sort { ($0.isOut == true && $1.isOut == false) && ($0.firstname < $1.firstname) && ($0.surname < $1.surname)}
Upvotes: 0
Views: 70
Reputation: 835
let new = [person,person1,person2,person3,person4]
var homeInArray = new
.filter { $0.isHome == true && $0.isInjured == true }
var homeNINArray = new
.filter { $0.isHome == true && $0.isInjured == false }
var newHomeArray = homeInArray.sort { $0.firstName < $1.firstName } + homeNINArray.sort { $0.firstName < $1.firstName }
Try it out this solution ...
Upvotes: 1