开发者

How to sort an NSMutableArray?

开发者 https://www.devze.com 2023-02-16 16:47 出处:网络
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.

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

精彩评论

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