通过不同属性订购核心数据实体,但在同一级别

发布于 2025-01-23 00:58:01 字数 910 浏览 1 评论 0原文

我有一个称为联系人的实体,其中包含多个属性,包括fivennameagrommynamefamilyname。我想通过fivenname订购它们,然后familyName。但是,某些实体可能没有fivennamefamilyName,但具有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2025-01-30 00:58:01

您需要的自定义越多,您必须编写的代码越多。

nssortDescriptor为自定义比较器提供了初始化器,

let myComparator : (Any, Any) -> ComparisonResult = { lhs, rhs in
    let lhs = lhs as! Contact
    let rhs = rhs as! Contact
    if !lhs.givenName.isEmpty { return lhs.givenName.localizedCaseInsensitiveCompare(rhs.givenName) }
    if !lhs.organizationName.isEmpty { return lhs.organizationName.localizedCaseInsensitiveCompare(rhs.organizationName) }
    return lhs.familyName.localizedCaseInsensitiveCompare(rhs.familyName)
}

let mySortDescriptor = NSSortDescriptor(key: nil, ascending: true, comparator: myComparator)

    
      

The more customization you need the more code you have to write.

NSSortDescriptor provides an initializer for a custom comparator, something like this

let myComparator : (Any, Any) -> ComparisonResult = { lhs, rhs in
    let lhs = lhs as! Contact
    let rhs = rhs as! Contact
    if !lhs.givenName.isEmpty { return lhs.givenName.localizedCaseInsensitiveCompare(rhs.givenName) }
    if !lhs.organizationName.isEmpty { return lhs.organizationName.localizedCaseInsensitiveCompare(rhs.organizationName) }
    return lhs.familyName.localizedCaseInsensitiveCompare(rhs.familyName)
}

let mySortDescriptor = NSSortDescriptor(key: nil, ascending: true, comparator: myComparator)

    
      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文