有一个未分组的NSMutableDictionary
{
A= "3";
B= "2";
C= "4";
}
我想要的结果是这样:
{
B= "2";
A= "3";
C= "4";
}
如何在objectivec中获得结果?
多谢~
有一个未分组的NSMutableDictionary
{
A= "3";
B= "2";
C= "4";
}
我想要的结果是这样:
{
B= "2";
A= "3";
C= "4";
}
如何在objectivec中获得结果?
多谢~
你不能通过值进行NSMutableDictionary 分组。如果改变NSArray的关键字和赋值的话,可以。代码如下:
NSMutableDictionary *results;
NSArray *sortedKeys = [results keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue])
return (NSComparisonResult)NSOrderedDescending;
if ([obj1 integerValue] < [obj2 integerValue])
return (NSComparisonResult)NSOrderedAscending;
return (NSComparisonResult)NSOrderedSame;
}];
NSArray *sortedValues = [[results allValues] sortedArrayUsingSelector:@selector(compare:)];
for (int s = ([sortedValues count]-1); s >= 0; s--) {
NSLog(@" %@ = %@",[sortedKeys objectAtIndex:s],[sortedValues objectAtIndex:s]);
}
for (int s = 0; s < [sortedValues count]; s++) {
NSLog(@" %@ = %@",[sortedKeys objectAtIndex:s],[sortedValues objectAtIndex:s]);
}