简体   繁体   English

按密钥中的字符串日期对NSDictionaries的NSArray进行排序?

[英]Sort NSArray of NSDictionaries by string date in key?

I have been struggling to find an easy way to sort an NSMutableArray that contains NSDictionaries. 我一直在努力寻找一种简单的方法来对包含NSDictionaries的NSMutableArray进行排序。 Each NSDictionary has a key named "Date" which contains an NSString of a date (ex: 10/15/2014). 每个NSDictionary都有一个名为“Date”的键,其中包含一个日期的NSString(例如:2014年10月15日)。

What I am looking to do is sort the array based on those strings in ascending order. 我要做的是根据这些字符串按升序对数组进行排序。

I have tried this with no luck: 我试过这个没有运气:

NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yy"];

    NSDate *d1 = [formatter dateFromString:s1];
    NSDate *d2 = [formatter dateFromString:s2];

    return [d1 compare:d2]; // ascending order
    return [d2 compare:d1]; // descending order
}

I have also tried this with no luck: 我也试过这个没有运气:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest"  ascending:YES];
    stories=[stories sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
    recent = [stories copy];

Each way results in a crash and I think it is because it is a NSString instead of a NSDate but I am just at a loss on how to do this. 每种方式都会导致崩溃,我认为这是因为它是NSString而不是NSDate,但我对如何做到这一点感到茫然。

Can anyone show me the correct way to accomplish this? 谁能告诉我正确的方法来实现这一目标?

This is how I call the first block of code: 这就是我调用第一个代码块的方式:

theDictionaries = [[theDictionaries sortedArrayUsingFunction:dateSort context:nil] mutableCopy];

You need to get the strings inside your dictionaries. 你需要在你的词典中找到字符串。 It looks like you're trying to compare the dictionaries themselves. 看起来你正试图比较字典本身。 If you had an array called data, you would do something like this, 如果你有一个名为data的数组,你会做这样的事情,

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];

self.data = [self.data sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    NSDate *d1 = [formatter dateFromString:obj1[@"date"]];
    NSDate *d2 = [formatter dateFromString:obj2[@"date"]];

    return [d1 compare:d2]; // ascending order
    return [d2 compare:d1]; // descending order
}];

NSLog(@"%@",self.data);

if you want to sort using Model Class then you must use it. 如果要使用Model Class进行排序,则必须使用它。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm a"];

finalCompleteArray = [finalCompleteArray sortedArrayUsingComparator:^NSComparisonResult(MessageChat *obj1, MessageChat *obj2) {
                        NSDate *d1 = [formatter dateFromString:obj1.LastDate];
                        NSDate *d2 = [formatter dateFromString:obj2.LastDate];

                        NSLog(@"[d1 compare:d2] %ld",(long)[d1 compare:d2]);
                        NSLog(@"[d2 compare:d1] %ld",(long)[d2 compare:d1]);
                        return [d2 compare:d1]; // descending order
                    }];
        NSLog(@"%@",finalAssignedArray);
  • Using finalCompleteArray is MutableArray 使用finalCompleteArrayMutableArray
  • Using MessageChat is Object Model Class File 使用MessageChat是对象模型类文件
  • Using obj1.LastDate is Object Model Class Variable 使用obj1.LastDate是对象模型类变量

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

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