If my tutorial help you then please donate, so that i continue writing free tutorials for every developer/programmer/freelancer
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: 10% [?]
Related posts:
- iPhone SDK Tutorial – {Part 4} Tips for UITableView Design [Add Header, Footer, Background Images & change Design] UITableView Tutorial: {Part 4} This tutorial will explain, how you...
- iPhone Programming Tips: Dialing number, opening Email & SMS applications Introduction iPhone SDK did not gives you full control over...
- iPhone Programming Tutorial: {Part 3} Grouped UITableView I am going to write series of UITableView tutorials (Video...
- iPhone Programming Tutorial {Part 7}: Adding Pictures into your table using Interface builder Creating iPhone Application: Introduction: I am going to write series...
- iPhone Tutorial for Retrieving Contact information from AddressBook Introduction In this tutorial I will explain how you can...
VRy interesting to read it