Friday, 5 August 2011

How to sort an array in ascending/descending order, easily!

So many people create their own functions to loop through an array and comparing the values to each other and sorting it that way, which takes far too much effort for me! No one seems to know about this on google, cause I searched for ages trying to find an easy way to do this. Simply do;

NSMutableArray *arrayOfLevelSaves = //your array full of numbers;
    
NSSortDescriptor * temp= [[NSSortDescriptor alloc] initWithKey:@"doubleValue" ascending:NO];
[arrayOfLevelSaves sortUsingDescriptors:[NSArray arrayWithObject:temp]];

No comments:

Post a Comment