简体   繁体   English

具有核心数据的复杂分类标准

[英]Complex sort criteria with core data

I'm trying to sort my records stored in core data with some problems. 我正在尝试对存储在核心数据中的记录进行排序,但存在一些问题。 In detail, I've two main issues: 详细地说,我有两个主要问题:

1# Sort condition based on child records: 1# 基于子记录的排序条件:

I have two entities (father and child) with a 1:M relationship, so one father can have more children. 我有两个具有1:M关系的实体(父亲和孩子),所以一个父亲可以生更多的孩子。 I would like to perform a sort of father records, based on the name of the older child. 我想根据年龄较大的孩子的名字来执行一种父亲记录。 So basically, I should perform a search on the child records for a certain father and retrieve the name of the child with the maximum age, and do a sort of the fathers based on this information. 因此,基本上,我应该对某个父亲的孩子记录进行搜索,并检索年龄最大的孩子的名字,然后根据此信息对父亲进行某种分类。 How can I write the sortdescriptor? 如何编写sortdescriptor?

2# Sort condition based on a calculation: 2# 基于计算的排序条件:

I have a list of addresses in my entity. 我有一个实体地址列表。 For each line I've a column for the corresponding latitude and one for the longitude. 对于每一行,我有一列对应的纬度,一列代表经度。 Based on the current position, I would like to sort these records from the nearest to the farthest. 根据当前位置,我想将这些记录从最近到最远进行排序。 Since the distance depends from the user position, it is a variable information and so it isn't stored in my core data table. 由于距离取决于用户位置,因此它是可变信息,因此不会存储在我的核心数据表中。 So, how to sort these records? 那么,如何对这些记录进行排序? What is the best approach? 最好的方法是什么?

Every suggestion will be really appreciated! 每个建议将不胜感激!

Thanks you in advance, yassa 预先感谢您,yassa

Could you use some compare operation on the result set? 您可以对结果集使用一些比较操作吗?

Compare method 比较方法

- (NSComparisonResult)compare:(Person *)otherObject {
    return [self.child compare:otherObject.child];
   }

NSArray *sortedArry;
sortedArry = [parentArray sortedArrayUsingSelector:@selector(compare:)];

BLOCKS

NSArray *sortedArry;
sortedArry = [parentArray sortedArrayUsingComparator:^(id a, id b) {
   NSString *first  = (Child*)a.name;
   NSDate   *second = (Child*)b.name;
   return [first compare:second];
}];

NSSortDescriptor NSSortDescriptor

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"child.name"
                                          ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArry;
sortedArry = [parentArray sortedArrayUsingDescriptors:sortDescriptors];

I am interested in seeing what your answer is. 我很想知道您的答案是什么。 I have had a similar issue. 我有一个类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM