【发布时间】:2012-06-26 23:36:21
【问题描述】:
我见过很多按键对字典进行排序,然后获取值的示例,但我将如何按值排序。
例如
{
blue:12;
red:50;
white:44;
}
我希望这些按数字降序排序:
{
red:50;
white:44;
blue:12
}
我尝试获取一个排序的 nsarray 键,我可以从中创建有序的 nsarray,但结果似乎仍然是无序的。
NSArray* sortedKeys = [stats keysSortedByValueUsingComparator:^(id first, id second) {
if ( first < second ) {
return (NSComparisonResult)NSOrderedAscending;
} else if ( first > second ) {
return (NSComparisonResult)NSOrderedDescending;
} else {
return (NSComparisonResult)NSOrderedSame;
}
}];
【问题讨论】:
标签: objective-c ios