Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 如何对NSMutableDictionary的NSMutableArray排序?_Cocoa_Sorting_Nsmutablearray_Nsmutabledictionary - Fatal编程技术网

Cocoa 如何对NSMutableDictionary的NSMutableArray排序?

Cocoa 如何对NSMutableDictionary的NSMutableArray排序?,cocoa,sorting,nsmutablearray,nsmutabledictionary,Cocoa,Sorting,Nsmutablearray,Nsmutabledictionary,我有NSMutableDictionary(NSString对象)的NSMutableArray。其中一个NSString对象实际上是一个日期,我需要根据该日期对NSMutableArray进行排序,我不希望它将日期排序为字符串。我该怎么做呢?您需要使用该方法。例如: NSInteger comparator( NSDictionary *d1, NSDictionary *d2, void *context ) { return [[d1 objectForKey:@"date"] com

我有NSMutableDictionary(NSString对象)的NSMutableArray。其中一个NSString对象实际上是一个日期,我需要根据该日期对NSMutableArray进行排序,我不希望它将日期排序为字符串。我该怎么做呢?

您需要使用该方法。例如:

NSInteger comparator( NSDictionary *d1, NSDictionary *d2, void *context )
{
  return [[d1 objectForKey:@"date"] compare:[d2 objectForKey:@"date"]];
}

// In some method:
NSArray *sortedArray = [array sortedArrayUsingFunction:comparator context:nil];

注意:这没有经过测试。

如果我理解正确,您的数组包含包含字符串的字典,您希望对这些字符串进行排序。。。作为日期。也许是这样的:

[someArray sortWithOptions: 0 usingComparator: ^(id inObj1, id inObj2) {
    NSDate      *date1 = [NSDate dateWithString: [inObj1 objectForKey: @"dateString"]];
    NSDate      *date2 = [NSDate dateWithString: [inObj2 objectForKey: @"dateString"]];

    return [date1 compare: date2];
}];

感谢您的回复,我会尝试。我也会考虑为您的数据模型构建类,因为您可以实现一个简单的比较:NSDATE对象的方法。使用这样的可变字典在经过某个点后往往会让人困惑。NSMutableArray没有“sortWithOptions”这样的方法。它是在10.6中引入的