Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

spaceman816

macrumors member
Original poster
Jul 29, 2009
30
0
I have a plist eastDict- I'm trying to make the keys into a sorted array but for some reason it's sorting incorrectly. The keys are: 1, 50, 100, 150, 200, 300, 400, 500.
I sorted the keys into an array like this:
Code:
NSArray *ranges = [[eastDict allKeys] sortedArrayUsingSelector: @selector(compare: )];

I eventually realized there was something wrong with the array, and so I looped through it and found that the keys were sorted as follows: 1, 100, 150, 200, 300, 400, 50, 500.

I have no clue as to why this would be.
 
I have a plist eastDict- I'm trying to make the keys into a sorted array but for some reason it's sorting incorrectly. The keys are: 1, 50, 100, 150, 200, 300, 400, 500.
I sorted the keys into an array like this:
Code:
NSArray *ranges = [[eastDict allKeys] sortedArrayUsingSelector: @selector(compare: )];

I eventually realized there was something wrong with the array, and so I looped through it and found that the keys were sorted as follows: 1, 100, 150, 200, 300, 400, 50, 500.

I have no clue as to why this would be.

What happens when you compare the NSString* @"100" and @"50" using compare: ?
 
Probably because it's comparing them as strings, not as numbers. So most likely it looks at the first character off each string. If they are the same then the second, otherwise it returns based on that character. So in character (and integer) comparison 1<5 so @"100" < @"50".

Implement your own comparison selector that converts the strings to numbers and compares them as numbers.
 
I have a plist eastDict- I'm trying to make the keys into a sorted array but for some reason it's sorting incorrectly. The keys are: 1, 50, 100, 150, 200, 300, 400, 500.
I sorted the keys into an array like this:
Code:
NSArray *ranges = [[eastDict allKeys] sortedArrayUsingSelector: @selector(compare: )];

I eventually realized there was something wrong with the array, and so I looped through it and found that the keys were sorted as follows: 1, 100, 150, 200, 300, 400, 50, 500.

I have no clue as to why this would be.

Because it's sorting them as strings.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.