Sort NSArray of NSDictionaries by string date in key?

懵懂的女人 提交于 2019-12-30 06:39:13

问题


I have been struggling to find an easy way to sort an NSMutableArray that contains NSDictionaries. Each NSDictionary has a key named "Date" which contains an NSString of a date (ex: 10/15/2014).

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.

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];

回答1:


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,

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);



回答2:


if you want to sort using Model Class then you must use it.

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
  • Using MessageChat is Object Model Class File
  • Using obj1.LastDate is Object Model Class Variable


来源:https://stackoverflow.com/questions/21445895/sort-nsarray-of-nsdictionaries-by-string-date-in-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!