Source: http://adeem.me/blog/2009/05/02/tutorial-tips-sorting-filtering-filter-nsmutablearray-and-sorting-nsmutablearray-by-ascending-or-objects-items-or-specified-key-or-key-value-or-by-nsnumber/
1. Sorting NSArray/NSMutableArray:
Sorting NSMutableArray is very simple:
NSMutableArray *arrayToFilter = [[NSMutableArray arrayWithObjects:@"Photoshop", @"Flex", @"AIR",@"Flash", @"Acrobat", @"<span id="IL_AD7" class="IL_AD">After Effects</span>", @"ColdFusion", @"Dreamweaver", nil] autorelease];
NSMutableArray *productsToRemove = [[NSMutableArray array] autorelease];
for (NSString *products in arrayToFilter) {
if (fliterText &amp;amp;&amp;amp; [products rangeOfString:fliterText options:NSLiteralSearch|NSCaseInsensitiveSearch].length == 0)
[productsToRemove addObject:products];
}
[arrayToFilter removeObjectsInArray:productsToRemove];
2. Creating NSMutableArray based on searched text in Array:
NSMutableArray *arrayToFilter = [NSMutableArray arrayWithObjects:@"Photoshop", @"Flex", @"AIR",@"Flash", @"Acrobat", @"After Effects", @"ColdFusion", @"Dreamweaver", nil];
NSMutableArray *productsToRemove = [NSMutableArray array];
for (NSString *products in arrayToFilter) {
if (fliterText &amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp; [products rangeOfString:fliterText options:NSLiteralSearch|NSCaseInsensitiveSearch].length == 0)
[productsToRemove addObject:products];
}
[arrayToFilter removeObjectsInArray:productsToRemove];
3. Sorting NSMutableArray (based on key) which contains objects
First, you’ll need to create an NSSortDescriptor and tell it which key to sort the array on.
First, you’ll need to create an NSSortDescriptor and tell it which key to sort the array on.
NSSortDescriptor *lastNameSorter = [[NSSortDescriptor alloc] initWithKey:@"lastName"ascending:YES];
[personList sortUsingDescriptors:[NSArray arrayWithObject:lastNameSorter]];