gpt4 book ai didi

ios - 如何对不同类别的数组进行排序?

转载 作者:行者123 更新时间:2023-11-28 12:48:47 24 4
gpt4 key购买 nike

我有两个 NSManagedObject 类“Person”和“Animal”。这两个类都有一个“名称”属性。如何将它们按字母顺序排列到一个数组中?

class Person: NSManagedObject {
@NSManaged var name:String?
@NSManaged var occupation:String?
}

class Animal: NSManagedObject {
@NSManaged var name:String?
@NSManaged var breed:String?
}

//This array is filled with both people and animal objects from my database
var peopleAndAnimals = [NSManagedObject]()

peopleAndAnimals.sort(//I want to sort alphabetically by name here)

最佳答案

试试这个:

peopleAndAnimals.sortInPlace {
if let name1 = $0.valueForKey("name") as? String,
name2 = $1.valueForKey("name") as? String {
return name1 < name2
} else {
return false
}
}

关于ios - 如何对不同类别的数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com