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!
分享到:
相关推荐
Pro Objective-C provides an in-depth, comprehensive guide to the language, its runtime, and key API’s. It explains the key concepts of Objective-C in a clear, easy to understand manner, and also ...
Whether you're an experienced C programmer or you're coming from a different language such as C++ or Java, leading Mac experts Scott Knaster and Waqar Malik show how to harness the power of Objective-...
It will show them how to take their existing language knowledge and design patterns and transfer that experience to Objective-C and the Cocoa runtime library. This is the express train to ...
Using a hands-on approach, you'll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
Explore the C language to learn how Objective-C works Learn how instances are created, and why they’re so important Tour the lifecycle of an Xcode project, from inception to App Store Discover how to...
How to build your first Objective-C application or two What is OOP or object oriented programming and how it impacts Objective-C What are the Objective-C language features and the foundational APIs ...
Using a hands-on approach, you'll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
Using a hands-on approach, you’ll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
More iPhone Development with Objective-C is an independent companion to Beginning iPhone Development with Objective-C. That is, it is a perfect second book, but it is also a great book for those ...
Whether you’re an experienced C programmer or you’re coming from a different language such as C++ or Java, leading Mac experts Mark Dalrymple and Scott Knaster show you how to harness the powers of ...
Pro Objective-C Design Patterns for iOS will teach you those design patterns that have always been present at some level in your code, but were never recognized, acknowledged, or fully utilized....
The iPhone is a sophisticated device, and mastering the Objective C language is the key to unlocking its awesome potential as a mobile computing platform. Objective C's concise, rich syntax and ...
how-to-code-in-python3 how-to-code-in-python3 how-to-code-in-python3
You should read this document to gain an understanding of how the Objective-C runtime system works and how you can take advantage of it. Typically, though, there should be little reason for you to ...
Learning-NumPy-Array-Supercharge-your-scientific-Python-computations-by-understanding-how-to-use-the-NumPy-library-effectively.pdf
著有多部程序员必读的经典著作,如How to Write Macintosh Software、Hacking Mac OS X Tiger和Macintosh Programming Secrets。 Waqar Malik UNIX迷,Macintosh控。早期曾就职于苹果公司,帮助开发Cocoa和Unix。...
An Objective-C Class for Working with Fractions 30 The @interface Section 33 Choosing Names 34 Class and Instance Methods 35 The @implementation Section 37 The program Section 39 Accessing Instance ...
You’ll learn how to work with the Xcode IDE, Objective-C’s Foundation library, and other developer tools such as Event Kit framework and Core Animation. Along the way, you’ll build example projects...