Arrays are structures used to hold a list of objects. Sometimes though you may want to sort the order that the elements appear.
Doing this is actually pretty simple once you know how, essentially you will be using the NSArray sortedArrayUsingSelector method.
For example, if you create an array like so
NSMutableArray *anArray = [[NSMutableArray alloc] init]; [anArray addObject:@"B"]; [anArray addObject:@"A"]; [anArray addObject:@"C"];
and then write out the contents of the array to the log using a foreach loop the results will look like this:
[Session started at 2009-03-25 16:57:55 -0400.] 2009-03-25 16:57:58.647 TipOfTheWeek[3403:20b] B 2009-03-25 16:57:58.648 TipOfTheWeek[3403:20b] A 2009-03-25 16:57:58.649 TipOfTheWeek[3403:20b] C
Obviously, the contents of the array stay in the same order in which they were inserted.
What you could do is create another array, sorted, using the sortedArrayUsingSelector method of NSArray. Here is how:
NSArray *sortedArray = [anArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
The odd (for some anyway) piece of this code is the @selector(caseInsensitiveCompare:) component of the code. This is a method passed to the function that instructions NSArray on how to sort the array.
At any rate, if you run through the array as before and print out the results to the log you will get this:
[Session started at 2009-03-25 17:07:18 -0400.] 2009-03-25 17:07:21.832 TipOfTheWeek[3537:20b] A 2009-03-25 17:07:21.833 TipOfTheWeek[3537:20b] B 2009-03-25 17:07:21.834 TipOfTheWeek[3537:20b] C
As you can see, the this array is sorted. There you have it!
Comments, questions – a better way to do this? Let me know in the comments below!
Here Is How To Fast Track Your Objective-C Programming Skills…
Click here or the box to the right to access to your online workshop now…
Each lesson comes packed with comprehensive video, source code and text. When appropriate I include hands-on exercises. Check out the list below to see what is specifically covered in each lesson:
Module 1 – Getting Started With iPhone App Development
- Lesson 1 – Overview of iPhone OS
- Lesson 2 – Introduction to Tools: XCode, Interface Builder & iPhone Simulator
- Lesson 3 – Your First App
- Lesson 4 – Super-Charge XCode
Module 2 – Learn How to Program in C
- Lesson 1 – What is Programming?
- Lesson 2 – C Programming Basics and Specifics
- Lesson 3 – Functions
- Lesson 4 – Variables and Arrays
- Lesson 5 – Program Flow
- Lesson 6 – Loops
- Lesson 7 – Complex Data with Struct
- Lesson 8 – Putting It All Together
Module 3 – Master Object Oriented Programming With Objective-C
- Lesson 1 – What is Object Oriented Programming?
- Lesson 2 – Objects
- Lesson 3 – More Strings, Lists and the For Each Loop
- Lesson 4 – Memory Management
- Lesson 5 – Designing Your Own Classes
- Lesson 6 – Extending Classes With Categories
- Lesson 7 – Protocols & Key-Value Coding
Module 4 – No-BS Cocoa-Touch With iPhone SDK
- Lesson 1 – Overview of Cocoa-Touch + Model-View-Controller
- Lesson 2 – Using Interface Builder (The View)
- Lesson 3 – Target-Action and the View in Code
- Lesson 4 – Delegation
- Lesson 5 – Super-Charging Your View With Interface Builder
- Lesson 6 – Model & App Architecture
Click here or the box to the right to access to your online workshop now
Comments on this entry are closed.