NSTableColumn compare selector

Hi All:


Is there a way to use NSNumericSearch as a parameter to a selector in a NSTableColumn?


For example, my data may be: BC-1, BC-2, BC-3... BC-9, BC-10, BC-11.


Obviously with just compare: as the selector, the data is sorted as: BC-1, BC-10, BC-11, BC-2... Not great and I don't want to force the user to enter the data as: BC-01, BC-02, etc...


I tried:


  NSTableColumn *col = [columnArray objectAtIndex:colIndex];
  NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"catalogNumber"
  ascending:YES
    comparator:^NSComparisonResult(NSString *catNum1, NSString *catNum2) {
    NSComparisonResult result = NSOrderedSame;
    result = [catNum1 compare:catNum2 options:(NSNumericSearch | NSCaseInsensitiveSearch)];
    return result;
    }];
  [col setSortDescriptorPrototype:sd];


But that doesn't work when you click on various, as it didn't seem to be called.


Is there a document or URL that describes how to get the sort I want?


Thanks very much


John

Depending on exactly what data you have and what you want to achieve, you can use -localizedStandardCompare:. This sorts just like the Finder would, which typically includes numeric sorting.


I would have expected the sort descriptor that you showed, using a comparator block, to work. Are you sure you set it at the right time? How was columnArray populated? Generally, you should find columns by identifier, not index, since tables can reorder columns or have hidden columns.


If you can't get that to work for some reason, you can create a category on NSString with a new comparison method. Be sure to put a unique-to-your-project prefix on the method name so it will never collide with an actual method on NSString, now or in the future. Have it just return the result from -compare:options: (or -compare:options:range:locale: with the locale specified, for locale-appropriate sorting), like your comparator block does.

Thanks for the information Ken. I'll try your suggestions and report back.


John

NSTableColumn compare selector
 
 
Q