gpt4 book ai didi

objective-c - 按 NSDate 对 NSMutableArray 中的 NSDictionaries 进行排序

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:43 25 4
gpt4 key购买 nike

我有一个带字典的 NSMutableArray,所有字典都包含一个 NSDate,它是 NSString 的形式,键为“Date”。我想按日期对 NSDictionaries 进行排序。因此,例如我有以下数组状态:

Dict
Date
20.06.1996 23:30
Dict
Date
04.10.2011 19:00
Dict
Date
20.06.1956 23:39

我想对它进行排序,使其看起来像这样:

Dict
Date
20.06.1956 23:39
Dict
Date
20.06.1996 23:30
Dict
Date
04.10.2011 19:00

我已经尝试过 NSSortDescriptor,但没有成功...

更新:

我已经设法对日期进行排序,但我遇到了这个问题:在字典中不仅有日期,还有其他对象,我的代码所做的只是在字典之间切换日期值,而不是切换周围的完整字典。有了这个,字典中的其他值被分配了一个错误的日期,这是非常糟糕的。有谁能够帮助我?这是我的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"];
NSMutableArray *d = [NSMutableArray arrayWithContentsOfFile:path];



for (int ii = 0; ii < [d count]; ii++) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
if (is24h) {
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
}
else {
[dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"];
}
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dat = [dateFormatter dateFromString:[[d valueForKey:@"Date"] objectAtIndex:ii]];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii];
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:dat forKey:@"Date"];
[d replaceObjectAtIndex:ii withObject:newDict];
[newDict release];
}


NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:YES];
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
[sorter release];
NSMutableArray *sorted = [NSMutableArray arrayWithArray:[d sortedArrayUsingDescriptors:sorters]];
[sorters release];
NSLog(@"%@",sorted);
for (int ii = 0; ii < [sorted count]; ii++) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
if (is24h) {
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
}
else {
[dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"];
}
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *sr = [dateFormatter stringFromDate:[[sorted valueForKey:@"Date"] objectAtIndex:ii]];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii];
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:sr forKey:@"Date"];
[sorted replaceObjectAtIndex:ii withObject:newDict];
[newDict release];
}
NSLog(@"before: %@"
""
"after: %@",d,sorted);
[sorted writeToFile:path atomically:YES];

最佳答案

有很多方法可以做到这一点,一种是使用 NSDate 对象而不是 NSString(或根据 ISO 8601 格式化的 NSString,以便字典排序与所需的排序相匹配)。然后你可以这样做:

NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date" 
ascending:YES];
[array sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];

或者,如果您不能(或不想)更改数据,您始终可以使用 block 进行排序:

[array sortUsingComparator:^(id dict1, id dict2) {
NSDate *date1 = // create NSDate from dict1's Date;
NSDate *date2 = // create NSDate from dict2's Date;
return [date1 compare:date2];
}];

当然,这可能会比第一种方法慢,因为您通常最终会创建超过 n 个 NSDate 对象。

关于objective-c - 按 NSDate 对 NSMutableArray 中的 NSDictionaries 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7628717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com