Monday, 16 June 2014

Sort array using function

NSInteger sortByDistance(id obj1, id obj2, void *context) {

    NSDictionary *dict1 =(NSDictionary*)obj1;
    NSDictionary *dict2 =(NSDictionary*)obj2;

    NSInteger num1 = [[dict1 objectForKey:@"distance"] integerValue];
    NSInteger num2 = [[dict2 objectForKey:@"distance"] integerValue];

    if (num1 < num2)
        return NSOrderedAscending;
    else if (num1 > num2)
        return NSOrderedDescending;

    return NSOrderedSame;

}


sortedArray = [anArray sortedArrayUsingFunction:sortByDistance context:nil];

reverseSortArray = [[[anArray sortedArrayUsingFunction:sortByDistance context:nil] reverseObjectEnumerator] allObjects];

No comments:

Post a Comment