0

On sorting an array i get : 1,10,2,3,4,5,6,7,8,9. What went wrong ?

My code was:

        NSArray *sortedArray = [optionKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

where optionKeys were: 7,3,8,4,9,5,1,6,2,10

i had also tried CaseInsensitiveCompare: and got the same result.

nitin k m p
  • 329
  • 2
  • 22
  • possible duplicate of [How to let the sortedArrayUsingSelector using integer to sort instead of String?](http://stackoverflow.com/questions/2752992/how-to-let-the-sortedarrayusingselector-using-integer-to-sort-instead-of-string) – Seamus Campbell Jun 01 '11 at 16:01

3 Answers3

1

String comparison will place 10 after 1. You'll need to use a number comparison function. This may help: How to let the sortedarrayusingselector using integer to sort instead of string

Community
  • 1
  • 1
Rob
  • 7,377
  • 7
  • 36
  • 38
0

You're doing a string sort rather than a numeric sort. See this answer.

Community
  • 1
  • 1
Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
0

Check out this answer. You want to do a numeric sort instead of a string sort.

Community
  • 1
  • 1
DHamrick
  • 8,338
  • 9
  • 45
  • 62