Basic Array Sort In Ascending Order
[SomeArrayName sortUsingSelector:@selector(compare:)];
SomeArrayName = [SomeArrayName sortedArrayUsingSelector:@selector(compare:)];
Sorting In Descending Order
NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
[SomeArrayName sortUsingDescriptors:[NSArray arrayWithObject: sortOrder]];
SomeArrayName = [SomeArrayName sortedArrayUsingDescriptors:[NSArray arrayWithObject: sortOrder]];
Sorting Classes
This can be used with classes that contain a varaible or object you want to sort the entire class by and for sorting multiple arrays by creating them inside the same class.
Declare the method in your .h file
NSInteger MyCompareFunction(id arg1, id arg2, void *arg3);
Create the method to return the sort order
NSInteger MyCompareFunction(id arg1, id arg2, void *arg3)
{
return [[(MyClassName *)arg2 SomeObjectInClass] compare: [(MyClassName *)arg1 SomeObjectInClass]];
//Swap arg1 and arg2 to reverse the sort order
}
Doing The Sort
[MyClassName sortUsingFunction:MyCompareFunction context: nil];