0

I have a NSArray of NSDictionaries, in which I create an NSArray of NSStrings from one of the elements in the NSDictionary.

I am then trying to sort both, but I am getting slightly different results from both sorts.

the NSDictionary is simple

NAME
Number

I am the first NSArray and creating a new NSArray using the NAME element of the dictionary.

I am then sorting the first array like this

NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"NAME" ascending:YES]; // 1
sortedByNameArray = [dataArrayOfDictionaries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

I then take the second array, which is exactly the same order before the first is sorted and sort it like this

// sorts nicknames
newSortedArray = [nameOnlyArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

And although both arrays are sorted alphabetically localizedCaseInsensitiveCompare: seems to do a good job of sorting where as my other sortedArrayUsingDescriptors: only seems to sort using the first letter so its not truly sorted.

I would like some help applying localizedCaseInsensitiveCompare to the NSArray of Dictionaries.. but I am not sure how to do this considering you have to access the NAME element of each NSDictionary inside the first NSArray.

Any help would be greatly appreciated.

1 Answer 1

2

To compare with localizedCaseInsensitiveCompare in the first case, use

NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"NAME" 
    ascending:YES 
    selector:@selector(localizedCaseInsensitiveCompare:)];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.