简体   繁体   中英

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 sort 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM