Sort NSStrings using caseInsensitiveCompare?
What I would like to achieve is: 1. Grab the NSStrings from all the NSDictionaries from my NSArray (Only one NSString per NSDictionary) 2. Then so开发者_开发知识库rt all the NSStrings so that all the Names are sorted alphabetically.
For example: Say there is Mark, Nick, and Rob. There are 6 NSDictionaries in my array (Each player having 2 nsdictionaries). So it would look like this (Randomized): Mark, Rob, Nick, Nick, Rob, Mark
I want to sort it like this: Mark, Mark, Nick, Nick, Rob, Rob
Is this possible? How would I do this?
Thanks!
What you first need is an array of the names. Let's assume that "dictionaryArray" is the name of your array of dictionaries, and that "name" is one of the keys in each of your dictionaries:
NSArray *names = [dictionaryArray valueForKey:@"name"];
Next, you sort this array. In this particular example, you're actually producing a sorted immutable copy of it:
NSArray *sortedNames = [names sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
If instead you wanted a mutable array to sort, after creating the array "names," you could make a mutable version of it:
NSMutableArray *mutableNames = [NSMutableArray arrayWithArray:names];
And then you could sort it in place with:
[mutableNames sortUsingSelector:@selector(caseInsensitiveCompare:)];
Good luck to you in your endeavors.
精彩评论