【发布时间】:2014-01-04 20:59:42
【问题描述】:
我正在尝试按降序对数组进行排序,我可以按升序对数组进行排序,这是我按升序排序的代码,
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"year" ascending:NO];
NSArray * descriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
sortedArray = [array sortedArrayUsingDescriptors:descriptors];
dictionaryWithSortedYears = [[NSMutableDictionary alloc] init];
NSString *tempDateKey = nil;
for (TreeXmlDetails *list in sortedArray)
{
id obj = [dictionaryWithSortedYears objectForKey:list.year];
if(!obj)
{
if(tempDateKey == nil)
{
arrayFromList = [[NSMutableArray alloc] init];
tempDateKey = list.year;
}
}
if([list.year isEqualToString:tempDateKey])
[arrayFromList addObject:list];
else
{
[dictionaryWithSortedYears setObject:arrayFromList forKey:tempDateKey];
tempDateKey = nil;
arrayFromList = nil;
arrayFromList = [[NSMutableArray alloc] init];
tempDateKey = list.year;
[arrayFromList addObject:list];
}
}
[dictionaryWithSortedYears setObject:arrayFromList forKey:tempDateKey];
NSArray *arr = [dictionaryWithSortedYears allKeys];
sortedKeys = [[NSArray alloc] initWithArray:[arr sortedArrayUsingSelector:@selector(compare:)]];
但是,我想对数组进行降序排序,请帮帮我。 提前致谢。
【问题讨论】:
标签: ios iphone objective-c nsarray nssortdescriptor