Question: We have a number of objects with titles and we want to sort them based on their titles. If we use the default search method:

```
NSArray *sortedObjects = [objects sortedArrayUsingComparator:
               ^NSComparisonResult(Object* a, Object* b)
               {
                   return [a.title compare:b.title];
               }];

```

It works fine, however, the results looks a bit weird if there are numbers in titles. For example, it will give us an array as the following: "Title 1" < "Title 10" < "Title 2" < ... What really want is: "Title 1" < "Title 2" < "Title 10" < ... Therefore, we switch to `NSNumericSearch` which gives us exactly what we are looking for. 

```
NSArray *sortedObjects = [objects sortedArrayUsingComparator:
            ^NSComparisonResult(Object* a, Object* b)
             {
                  return [a.title compare:b.title options:NSNumericSearch];
             }];

```

Then we run into a big performance issue. We did some profiling and notice that the second method is about 35 times slower than the first one. 

**What is it that making numerally searching so much slower? Do you have any recommendations upon how to get numerically sorted results faster?**

Thanks~

Answer: 
> 
> Do you have any recommendations upon how to get numerically sorted results faster?
> 
> 
> 

The solution that you should really be using is described in [Sorting is not working correctly](https://stackoverflow.com/q/13229892/643383), i.e. use [`-localizedStandardCompare:`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/#//apple_ref/occ/instm/NSString/localizedStandardCompare:) to sort your strings. That method will sort strings the way a user would typically expect to see them:

```
file1
file2
file3
...
file10
file11

```

From the docs:

> 
> This method should be used whenever file names or other strings are
>  presented in lists and tables where Finder-like sorting is
>  appropriate. The exact sorting behavior of this method is different
>  under different locales and may be changed in future releases.
> 
> 
> 

It's a good bet that this solution will be both faster and more correct than your current solution. Using `NSNumericSearch` runs the risk of ignoring differences in the non-numeric parts of the string, so that e.g. "foo1" appears before "bar2".
Sorry I may have misled a lot of you guys there. 

The reason why my numerical search is slower is because that the data set I get is *almost* sorted alphabetically. Therefore, I believe whatever search algorithm that [`sortedArrayUsingComparator`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/#//apple_ref/occ/instm/NSArray/sortedArrayUsingComparator:) is using has already taken this into consideration. This is the reason why using `[a.title compare:b.title]` is faster then `[a.title compare:b.title options:NSNumericSearch]`. However, it is not alway faster. 

Here is a bit of profiling I did with our data set. *Literal search* is slower than *numeric search*. And with localizedStandardCompare, it seems to be even faster. The result is generated with relatively random data set of 21812 objects. 

![enter image description here](https://i.stack.imgur.com/fMSzM.png)