0

I have a list of data which I am able to sort by clicking on the column header. The only problem is that it displays like:

10A
11A
12A
1A
2A
3A

etc...

Where ideally, I would like it to be sorted like:

1A
2A
3A
4A
5A
6A
7A
8A
9A
10A
11A

etc...

Is there any way to get around this default sorting action?

These are my sort descriptors properties:

Sort Descriptors Properties

@Monolo:

enter image description here

    self.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"self"
                                                       ascending:YES
                                                      comparator:^NSComparisonResult(id obj1, id obj2) {
                                                          NSLog(@"COMPARATOR!");
                                                          return [obj1 compare:obj2
                                                                       options:NSNumericSearch];
                                                      }]];
Alex
  • 3,031
  • 6
  • 34
  • 56
  • http://stackoverflow.com/questions/8242735/how-to-sort-array-controller-alphabetically-with-numbers-last-in-objective-c – Rohit Dhawan Feb 13 '13 at 11:27

1 Answers1

0

In general, the kind of sorting you are looking for can be obtained through the compare:options: method of NSString, so if you construct a sort descriptor in code and get them into your array controller (programmatically or by binding from the nib file), you should get what you are looking for.

Set up a sort descriptor array, for instance like this:

// AppDelegate.h:

@property (strong) NSArray *sortDescriptors;


//AppDelegate.m:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"self"
                                                ascending:YES
                                               comparator:^NSComparisonResult(id obj1, id obj2) {
                                                   return [obj1 compare:obj2
                                                                options:NSNumericSearch];
                                               }]];
}

So instead of binding to the Standard User Defaults Controller, you should bind to your App Delegate (or some other object that you prefer to hold these helper objects), and you get the behaviour you are after. Model key path for the binding is sortDescriptors using the code snippet above.

Monolo
  • 18,205
  • 17
  • 69
  • 103
  • Do I change the sort descriptors for the table, or the Array Controller? – Alex Feb 13 '13 at 12:18
  • The result is unexpected when I use that code. I've edited the original post to include a sceenshot, – Alex Feb 14 '13 at 18:06
  • I can confirm that the NSLog statement isnt being called, but the appdidfinishlaunching fucntion is. This is the code with the NSLog in (Appended to question). – Alex Feb 14 '13 at 18:23
  • I'd forgot to bind to the App Delegate. Now though, the list is in a seemingly random order. Should the Controller key be anything? – Alex Feb 14 '13 at 22:40