NSSortDescriptor 可以使用部分键进行排序吗?

发布于 2024-10-17 22:08:20 字数 597 浏览 1 评论 0原文

我有一个 CoreData iPhone 应用程序,带有显示地址的视图。

地址如下所示:“5 Blob Street, 2222, Suburbia”,其中“5 Blob Street”是 address_and_number 键,“2222”是邮政编码,“Suburbia”是郊区。

目前我使用这个 NSSortDescriptor,它工作得很好:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
                                    initWithKey:@"address_and_number" ascending:YES];

但是,我的客户想要按街道名称排序,即在 address_and_number 键之外,将评估“Blob Street”。

有没有办法进行这种排序,而无需将address_and_number键拆分为两个键“address”和“number”?

我是否可以使用正则表达式来仅获取排序所需的 address_and_number 键部分,并以某种方式将其添加到 NSSortDescriptor 对象中?

I have a CoreData iphone application with a view that display addresses.

An address looks like this: "5 Blob Street, 2222, Suburbia", where "5 Blob Street" is the address_and_number key, "2222" the post code, and "Suburbia" the suburb.

Currently I use this NSSortDescriptor, which works fine:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
                                    initWithKey:@"address_and_number" ascending:YES];

However, my client wants to sort by street name, i.e. out of the address_and_number key, "Blob Street" would be evaluated.

Is there any way to do such sorting without resorting to splitting the address_and_number key into two keys, "address" and "number"?

Can I use perhaps a regular expression to grab only the part of the address_and_number key I need for sorting, and somehow add that to the NSSortDescriptor object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

岁月打碎记忆 2024-10-24 22:08:20

根据相关问题的答案 使用类别和核心数据进行自定义排序 - 框架支持吗?

NSFetchedResultsController 内部的排序是在持久存储级别执行的,因此如果您使用 SQLite 作为后端,它将失败。

对于这样的事情,我会对数据进行非标准化处理,并除了实际的出生日期之外还存储月份和日期,以便您可以对它们进行排序。

在尝试了选择器和比较器之后,没有一个作为我使用 sqlite3 的持久级别起作用,我求助于将address_and_number键拆分为address_street_name和address_street_number键,然后按address_street_name排序。

According to the answer at a relevant question at Custom sorting using categories and core data - is it supported by the framework?

Sorting inside of the NSFetchedResultsController is performed at the persistent store level so if you are using SQLite as the backend it will fail.

For something like this I would de-normalize the data and store the month and day of month in addition to the actual birthdate so that you can sort on them.

After trying both a selector and a comparator, none of which worked as my persistent level I am using sqlite3, I resorted to splitting the address_and_number key into address_street_name and address_street_number keys, and then sorting by address_street_name.

往昔成烟 2024-10-24 22:08:20

尝试将 NSComparator 块添加到 NSSortDescriptor 的初始化中,如下所示:

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                    initWithKey:@"Address_suburb" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
                                      // do your comparison without house numbers here
                                      // return either NSOrderedAscending, NSOrderedDescending or NSOrderedSame
                                    }];

Try adding a NSComparator block to the initialization of the NSSortDescriptor like this:

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                    initWithKey:@"Address_suburb" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
                                      // do your comparison without house numbers here
                                      // return either NSOrderedAscending, NSOrderedDescending or NSOrderedSame
                                    }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文