Tutorial/Tips (Sorting & Filtering): Filter NSMutableArray and Sorting NSMutableArray by Ascending or Objects or Specified key or Key Value or by NSNumber

 Tutorial/Tips (Sorting & Filtering): Filter NSMutableArray and Sorting NSMutableArray by Ascending or Objects or Specified key or Key Value or by NSNumber
I think its time for me to write about tips regarding iPhone SDK. Its always hard for me to remember each line of code for small task(i.e getting contact’s emails from AddressBook). Today I think, I should put small code in some order. All tips which I am going to post on this blog didn’t belongs to me.

I try to post one or two tips every day to help peoples and to let myself find it easily on my blog. Sorting objects held in an NSMutableArray is a pretty common task while developing application for Mac os X or iPhone. So today I am going to write about Sorting NSArray/NUMutableArray ascending, Filter NSMutableArray based on searched character/string and Sorting an object List based on key value.
Note: You can find the working copy of this code below.

1. Sorting NSArray/NSMutableArray:

Sorting NSMutableArray is very simple:

NSMutableArray *arrayToFilter = [[NSMutableArray arrayWithObjects:@"Photoshop", @"Flex", @"AIR",@"Flash", @"Acrobat", @"After Effects", @"ColdFusion", @"Dreamweaver", nil] autorelease];

	NSMutableArray *productsToRemove = [[NSMutableArray array] autorelease];
	for (NSString *products in arrayToFilter) {
		if (fliterText && [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 && [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.

 NSSortDescriptor *lastNameSorter = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
[personList sortUsingDescriptors:[NSArray arrayWithObject:lastNameSorter]];

You can sort the list based on NSNumber as well. If you want to sort on multiple keys, then you can create a sort descriptor for each key and specify whether or not you want them to sort ascending or descending.

You can grab the code here. I will write more about NSMutableArray in next topics.

Popularity: 8% [?]

My name is Adeem M Basraa. I am a software engineer from Lahore, PK. I’ve been involved in software development for nearly 4 years, with the last 1 year focused on application development for mobile devices (iPhone & Android).

One Comment on "Tutorial/Tips (Sorting & Filtering): Filter NSMutableArray and Sorting NSMutableArray by Ascending or Objects or Specified key or Key Value or by NSNumber"

  1. VRy interesting to read it :P

Got something to say? Go for it!