37

I have an array of objects and I need them sorted by their "title" key. It's currently working, though it's using an ASCII sort instead of a natural sort. The titles are filenames, so they look like this:

file1
file2
file3
...
file10
file11
file12

I'm getting, as you would expect:

file1
file10
file11
file12
file2
file3
...

Does anyone know if there is a way built-in to the NSArray sorting functionality to get this natural sorting as opposed to the alphabetical sort? I found some generic algorithms, but I was hoping for something built-in.

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
Cory Imdieke
  • 14,140
  • 8
  • 36
  • 46

5 Answers5

54

NSStrings can be compared using the NSNumericSearch compare option.

One version:

NSInteger sort(Obj* a, Obj* b, void*) {
    return [[a title] compare:[b title] options:NSNumericSearch];
}

result = [array sortedArrayUsingFunction:&sort context:nil];

Or a bit more generic:

NSInteger sort(id a, id b, void* p) {
    return [[a valueForKey:(NSString*)p] 
            compare:[b valueForKey:(NSString*)p]
            options:NSNumericSearch];
}

result = [array sortedArrayUsingFunction:&sort context:@"title"]

Or using blocks:

result = [array sortedArrayUsingComparator:^(Obj* a, Obj* b) { 
    return [[a title] compare:[b title] options:NSNumericSearch]; 
}];
Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
25

Those attempting to replicate the Finder's sorting behavior on Mac OS X 10.6 and later and iOS 4 and later can use localizedStandardCompare: as the comparator selector.

Those with the same goal on earlier versions of Mac OS X and/or iOS should use the solution described in the String Programming Guide.

Akshet
  • 533
  • 5
  • 16
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
4

Based on @PeterHosey's answer, if you're working with an array of string objects:

SHORT ANSWER:

NSArray *orderedTitles = [unorderedTitles sortedArrayUsingSelector:@selector(localizedStandardCompare:)];

FULL EXAMPLE:

NSArray *unorderedTitles = @[
    @"file12",
    @"file1",
    @"file10",
    @"file3",
    @"file2",
    @"file11"
];

NSArray *orderedTitles = [unorderedTitles sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
NSLog(@"orderedTitles = %@", orderedTitles);
/*
orderedTitles = (
    file1,
    file2,
    file3,
    file10,
    file11,
    file12
)
*/
John Erck
  • 9,478
  • 8
  • 61
  • 71
0

This works on iOS

NSString *name0 = @"winter";
NSString *name1 = @"summer";
CFComparisonResult result = CFStringCompareWithOptions(
    (CFStringRef) name0,
    (CFStringRef) name1,
    CFRangeMake (0, CFStringGetLength((CFStringRef) name0)),
    kCFCompareCaseInsensitive | kCFCompareLocalized | kCFCompareNumerically
);
neoneye
  • 50,398
  • 25
  • 166
  • 151
0

You can also do the sorting in the following way:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES selector:@selector(localizedStandardCompare:)];
NSArray *sortedItems = [unsortedItems sortedArrayUsingDescriptors:@[sortDescriptor]];
gypsicoder
  • 5,416
  • 5
  • 24
  • 38