Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a natural sort on an NSArray?

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.

like image 591
Cory Imdieke Avatar asked May 17 '10 02:05

Cory Imdieke


People also ask

Is Nsarray ordered?

An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.

Can Nsarray contain nil?

arrays can't contain nil.


2 Answers

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];  }]; 
like image 200
Georg Fritzsche Avatar answered Sep 23 '22 13:09

Georg Fritzsche


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.

like image 22
Peter Hosey Avatar answered Sep 20 '22 13:09

Peter Hosey