核心数据获取请求的复杂排序 [英] Complex sorting for core-data fetch request
问题描述
我有一个属性为的
NSEntity
( Cloth
c $ c>),它从预定义的 NSString
值填充,即Poncho,Jacket,Coat,Gown,Top,Suit等。
I have an NSEntity
(Cloth
) with an attribute (type
) which is filled from a list of predefined NSString
values i.e. Poncho, Jacket, Coat, Gown, Top, Suit, etc.
我想获取所有的Cloth记录,使记录基于 type
的定义顺序排序。即,所有Poncho的记录在顶部,然后是夹克,然后是大衣,然后是大衣,然后是顶部,然后是西装等。
I want to fetch all the Cloth records such that the records are ordered based on a defined order for type
. i.e. All the records for Poncho are on top, then Jacket, then Coat, then Gown, then Top, then Suit, etc.
Poncho …
Poncho …
Poncho …
Jacket …
Jacket …
Coat …
Coat …
Coat …
Gown …
Gown …
Top …
Top …
Top …
Top …
Suit …
Suit …
此订单不是升序或降序,而是自定义。如何获取 Cloth
记录,以便以此(定义)顺序返回?
This order is not ascending or descending, rather it's custom. How can I fetch the Cloth
records such that they are returned in this (defined) order?
推荐答案
实现自定义顺序的唯一方法是引入 NSNumber
类型的新属性来跟踪订单。
The only way to implement a custom order is to introduce a new attribute of type NSNumber
to keep track of the order.
[似乎在类别字符串(Poncho等)后面有更多的文本,所以你实际上有很多唯一的字符串, ]
[It seems that there is more text after the category string (Poncho, etc), so that you actually have many unique strings that all have to follow a custom order, only that they are grouped by first word.]
如果你真的想将这个逻辑与其他数据分开,你可以创建一个新的实体与属性
实体。 name
和 sortOrder
的ClothType Cloth
If you really want to separate this logic from the rest of the data, you can create a new entity ClothType
with attributes name
and sortOrder
and have a relationship with your Cloth
entity.
另一种方案是将排序属性乘以每个主要类别的10000,并为同一类别的每个后续类别添加1。
Another scheme is to multiply the sort attribute with, say, 10000 for every "primary" category and add 1 for each subsequent one of the same category.
这篇关于核心数据获取请求的复杂排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!