Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sortedArrayUsingComparator int

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?

like image 466
Vins Avatar asked Jun 03 '11 13:06

Vins


1 Answers

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;
    }
}
like image 72
Deepak Danduprolu Avatar answered Sep 19 '22 15:09

Deepak Danduprolu