How to sort an NSMutableArray?
I want to sort an NSMutableArray. The following is the structure of the array.
An array includes some dictionaries those have two variables with keys like this.
NSMutableArray array - NSDictionary dic - int num(key: @"Num"), NSString str(key: @"Str")
How do I sor开发者_Go百科t the array by the num value of the dictionary?
[array sortUsingComparator:^(id dic1, id dic2) {
return [dic1 objectForKey:@"Num"] <= [dic2 objectForKey:@"Num"];
}];
but you really should use NSNumber
instead of int
for @"Num"
and use
[array sortUsingComparator:^(id dic1, id dic2) {
return [[dic1 objectForKey:@"Num"] intValue] <= [[dic2 objectForKey:@"Num"] intValue];
}];
instead.
精彩评论