Wednesday, 21 May 2014

Sorting an NSArray in ascending order

 NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            if ([obj1 intValue] < [obj2 intValue]) return NSOrderedAscending;
            else return NSOrderedDescending;
        }];
NSLog(@"sorted ~~~~~~~ %@",sorted);

(OR)

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [array_value_name sortedArrayUsingDescriptors:sortDescriptors];

No comments:

Post a Comment