NSArray* sortedArray = [myArray sortedArrayUsingSelector:@selector(compare:)];
Just wondering how I can sort this array of numbers in descending order?
In the Swift array, we can reverse the array. To reverse the array we use the reverse() function. This function reverses the order of the specified array. It is the easiest way to reverse the array.
The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...
Either use the method that allows you to pass a block as a comparator and implement a comparator that reverses the objects (returns NSOrderedAscending for NSOrderedDescending and vice versa)....
... or:
NSArray *reversed = [[[myArray sortedArrayUsingSelector:@selector(compare:)] reverseObjectEnumerator] allObjects];
Sorry. Derped the most important part.
Use NSSortDescriptor
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSArray *temp = [myArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
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