订购一组对象?

Order an array of objects?

我在我的项目中创建了两个 NSManagedObject,名为 ContactGroups。两个对象都有一个名为 timeLastMessageReceived 的 属性。 我有一个包含这两个对象的数组。

我想按 timeLastMessageReceived 的时间排序该数组。

@interface Contact : NSManagedObject

...

@property (nonatomic, retain) NSDate * timeLastMessageReceived;
@property (nonatomic, retain) NSString * lastMessage;

@end



@interface Groups : NSManagedObject

@property (nonatomic, retain) NSDate * timeLastMessageReceived;
@property (nonatomic, retain) NSString * lastMessage;

@end

我正在尝试这个方法:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSArray *newArr = [self.chatArray sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSDate *date1 = obj1[@"timeLastMessageReceived"];
        NSDate *date2 = obj2[@"timeLastMessageReceived"];
        return [date1 compare:date2];
    }];

但它因错误而崩溃:-[Contact objectForKeyedSubscript:]: unrecognized selector sent to instance
有任何想法吗。谢谢。

您可以在 NSDictionary 的实例上使用 [] 运算符(即 -objectForKeyedSubscript:)。如错误消息所述,您正在处理 Contact 的实例,因此它不支持下标运算符。

您展示了 timeLastMessageReceived 如何只是 Contact 上的 属性,因此您应该能够使用默认的 属性 访问语法:

    NSDate *date1 = obj1.timeLastMessageReceived;
    NSDate *date2 = obj2.timeLastMessageReceived;

更新的答案:

作为替代方案,您可以使用排序描述符对数组进行排序:

NSSortDescriptor *timeLastMessageReceivedDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeLastMessageReceived" ascending:YES];
NSArray *sortDescriptors = @[timeLastMessageReceivedDescriptor];
NSArray *sortedArray = [self.chatArray sortedArrayUsingDescriptors:sortDescriptors];

原回答:

我猜 self.chatArray 包含类型 Contact 的实例?你应该能够像那样实现比较器:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSArray *newArr = [self.chatArray sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(Contact *contact1, Contact *contact2) {
    NSDate *date1 = contact1.timeLastMessageReceived;
    NSDate *date2 = contact2.timeLastMessageReceived;
    return [date1 compare:date2];
}];

我将创建一个由 Contact 和 Group 实现的协议,该协议定义 timeLastMessageReceived 属性.

@protocol ContactOrGroupProtocol
@property (nonatomic, retain) NSDate * timeLastMessageReceived;
@end

那么,比较器块将是:

^NSComparisonResult(id<ContactOrGroupProtocol> obj1, id<ContactOrGroupProtocol> obj2) {
    NSDate *date1 = obj1.timeLastMessageReceived;
    NSDate *date2 = obj2.timeLastMessageReceived;
    return [date1 compare:date2];
}];

另一种方法是在classGroupsContact

中实现比较方法
- (NSComparisonResult)compareLastMessageReceived:(Groups *)group {
  return [self.timeLastMessageReceived compare:group.timeLastMessageReceived];
}

- (NSComparisonResult)compareLastMessageReceived:(Contact *)contact {
  return [self.timeLastMessageReceived compare:contact.timeLastMessageReceived];
}

并调用 sortedArrayUsingSelector:

NSArray *newArr = [self.chatArray sortedArrayUsingSelector:@selector(compareLastMessageReceived:)];