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!
分享到:
相关推荐
著有多部程序员必读的经典著作,如How to Write Macintosh Software、Hacking Mac OS X Tiger和Macintosh Programming Secrets。 Waqar Malik UNIX迷,Macintosh控。早期曾就职于苹果公司,帮助开发Cocoa和Unix。...
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 ...
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...
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 ...
- **Objective-C Classes, Objects, and Methods**: Provides a detailed look at how to define and use classes, objects, and methods in Objective-C. - **Diving into Objective-C**: Explores more advanced ...
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....
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...
How-to-Make-a-Computer-Operating-System, 在 C 中,如何制作计算机操作系统 如何使计算机操作系统关于如何从头开始编写 C/C 操作系统的在线书籍。注意 : 这个存储库是我旧课程的一个。 我的第一个项目是我在高中时...
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...
Cocoa and Objective-C: Up and Running offers just enough theory to ground you, then shows you how to use Apple's rapid development tools -- Xcode and Interface Builder -- to develop Cocoa applications...
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 ...
“When the folks at John Wiley & Sons approached me about writing Objective-C Programming For Dummies, I thought long and hard about it. Within 480 pages, I wanted to be sure that I could explain to ...
eu-16-Gonzalez-How-To-Fool-An-ADC-Part-II-Or-Hiding-Destruction-Of-Turbine-With-A-Little-Help-Of-Signal-Processing 安全架构 安全体系 信息安全研究 AI web安全
infineon的CY系列芯片使用interrupt的手册