0

I'm currently using this compare options to sort a list of strings:

 [self compare:aString options:NSNumericSearch|NSCaseInsensitiveSearch];

However, numbers come before than symbols:

99
[Hello
Hello

Is there a way to sort symbols before numbers ? thanks

aneuryzm
  • 63,052
  • 100
  • 273
  • 488
  • There should be some useful information in the answers to this question: http://stackoverflow.com/questions/9327997/dictionary-key-sort-options-alpha-then-numeric – Monolo Jun 13 '12 at 09:23

1 Answers1

0

Not all symbols come before numbers in the ASCII table, which is why you noticed this behaviour with '['. Assuming self in your example is an NSArray, you'll need to use the sortedArrayUsingComparator: method and implement a custom sorter. (Other classes besides NSArray will have this method as well.)

NSComparator sortBlock = ^(id string1, id string2)
{
    // Fill this in
};

NSArray* sortedArray = [originalArray sortedArrayUsingComparator:sortBlock];

Check out Getting Started with Blocks on Apple's developer site.

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128