开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜