NSSortDescriptor 可以使用部分键进行排序吗?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据相关问题的答案 使用类别和核心数据进行自定义排序 - 框架支持吗?
在尝试了选择器和比较器之后,没有一个作为我使用 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?
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.
尝试将 NSComparator 块添加到 NSSortDescriptor 的初始化中,如下所示:
Try adding a NSComparator block to the initialization of the NSSortDescriptor like this: