我有数组中的对象我想排序
let person = Person()
person.isHome = true
person.firstname = "Foo"
person.surname = "Bar"
等...
我取了所有的人
guard let allPeople = controller.fetchedObjects as? Array<Person> else { return }
现在我想过滤和排序
var homeArray = allPeople
.filter { $0.isHome == true }
.sort { $0.surname < $1.surname }
一切都很好...除了,如果我想要额外的&#39;排序描述符&#39;像这样...
var homeArray = allPeople
.filter { $0.isHome == true }
.sort { $0.surname < $1.surname }
.sort { $0.firstname < $1.firstname }
.sort { $0.isOut == true && $1.isOut == false }
我仍然希望他们按姓氏排序,但我也希望他们在姓氏之后按名字排序并且在非受伤之上受伤...这是什么语法?我似乎无法在任何地方找到它......我是否必须回到使用NSArray?
是否像
一样简单.sort { ($0.isOut == true && $1.isOut == false) && ($0.firstname < $1.firstname) && ($0.surname < $1.surname)}
答案 0 :(得分:1)
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 }
试试这个解决方案......