I have an array of a dictionary that it sortedArrayUsingComparator sorts them based on the key;
listPerc = [listItem sortedArrayUsingComparator: ^(id a, id b) {
NSString *first = [a objectForKey:@"perc"];
NSString *second = [b objectForKey:@"perc"];
return [first compare:second];
the problem is that this key is a numbers, and order is so for example: 1,10, 15, 17, 2, 23, etc. it does not calculate the magnitude of the number. how can I do?
Can't you return the comparison result like this,
listPerc = [listItem sortedArrayUsingComparator: ^(id a, id b) {
int first = [[a objectForKey:@"perc"] intValue];
int second = [[b objectForKey:@"perc"] intValue];
if ( first < second ) {
return (NSComparisonResult)NSOrderedAscending;
} else if ( first > second ) {
return (NSComparisonResult)NSOrderedDescending;
} else {
return (NSComparisonResult)NSOrderedSame;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With