通过不同属性订购核心数据实体,但在同一级别
我有一个称为联系人
的实体,其中包含多个属性,包括fivenname
,agrommyname
和familyname
。我想通过fivenname
订购它们,然后familyName
。但是,某些实体可能没有fivenname
或familyName
,但具有agrommy> agrommistionName
。在排序联系人
时,我想将实体订购为agrommandname
and fivenname
,其中相同的属性,例如,如果没有gishivename,则使用组织名称。
目前我有:
let fetchRequest: NSFetchRequest<Contact> = Contact.fetchRequest()
fetchRequest.sortDescriptors = [
NSSortDescriptor(key: "givenName", ascending: true),
NSSortDescriptor(key: "organizationName", ascending: true),
NSSortDescriptor(key: "familyName", ascending: true)
]
我会写类似的东西:
order by IS_EMPTY(IS_EMPTY(givenName, lastName), organizationName) ASC
如何用Swift和Core Data表达出来?
I have an entity called Contact
with multiple attributes, including givenName
, organizationName
and familyName
. I want to order them by givenName
and then familyName
. However, some entities may not have givenName
or familyName
but have organizationName
. When sorting the Contacts
I want to order the entities as organizationName
and givenName
where the same attribute, e.g. use organizationName when givenName is not available.
Currently I have:
let fetchRequest: NSFetchRequest<Contact> = Contact.fetchRequest()
fetchRequest.sortDescriptors = [
NSSortDescriptor(key: "givenName", ascending: true),
NSSortDescriptor(key: "organizationName", ascending: true),
NSSortDescriptor(key: "familyName", ascending: true)
]
I would write something similar like:
order by IS_EMPTY(IS_EMPTY(givenName, lastName), organizationName) ASC
How can I express that with Swift and Core Data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的自定义越多,您必须编写的代码越多。
nssortDescriptor
为自定义比较器提供了初始化器,The more customization you need the more code you have to write.
NSSortDescriptor
provides an initializer for a custom comparator, something like this