How to sort NSMutable array in ascending order. Can any one help me out this.
4 Answers
Here is a way to sort an array of 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];
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 that you want to sort.
2 Comments
Lily Ballard
You didn't even specify a key.
Gypsa
I have done this code with sorting array which contain numbers so there were no key and mostly the arrays dont have key that why.But no matters It's good that you got the solution ....happy coding...:)