How to sort an NSMutable array in ascending order

How to sort an NSMutable array in ascending order. Can anyone help me on this.

+4
source share
4 answers

You did not say in your array, but if this is something that answers -compare: then you can use

 [myArray sortUsingSelector:@selector(compare):]; 
+4
source

Here is a way to sort an array of an object

  NSSortDescriptor * descFirstname = [[NSSortDescriptor alloc] initWithKey:@"firstname" ascending:YES]; NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"lastname" ascending:YES]; [myArrayOfPerson sortUsingDescriptors:[NSArray arrayWithObjects:descLastname,descFirstname, nil]]; [descFirstname release]; [descLastname release]; 
+2
source
 NSSortDescriptor *sortDescriptor; sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:nil ascending:YES] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSArray *sortedArray; sortedArray = [sortValuesArray sortedArrayUsingDescriptors:sortDescriptors]; NSLog(@"sortedArray%@",sortedArray); 

sortValuesArray is the array you want to sort.

+1
source

All Articles