Gianfranco Cotumaccio
Gianfranco Cotumaccio

Reputation: 57

sorting array and objetct

How can I order these values ​​in the array in reverse order?

_gradientArrayToWhite = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", nil];

Upvotes: 1

Views: 80

Answers (2)

Janmenjaya
Janmenjaya

Reputation: 4163

NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:8],[NSNumber numberWithInt:9],[NSNumber numberWithInt:10], nil];

Here You can use your array directly also like this

NSArray *arr1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", nil];


NSArray *revArr = [[arr reverseObjectEnumerator] allObjects];

Result = <__NSArrayM 0x71348e0>(
 10,
 9,
 8,
 7,
 6,
 5,
 4,
 3,
 2,
 1
)

Upvotes: 2

pNre
pNre

Reputation: 5376

Using one of the sorting methods of NSArray

NSArray * sorted = [_gradientArrayToWhite sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [obj1 integerValue] < [obj2 integerValue];
}];

Upvotes: 5

Related Questions