Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSArray of strings sort issue [duplicate]

So i have the following code:

NSArray *pathArray = @[@"path/Documents/page9.png",
    @"path/Documents/page8.png",
    @"path/Documents/page10.png",
    @"path/Documents/page11.png",
    @"path/Documents/page0.png",
    @"path/Documents/page12.png",
    @"path/Documents/page6.png",
    @"path/Documents/page4.png",
    @"path/Documents/page5.png",
    @"path/Documents/page1.png",
    @"path/Documents/page7.png",
    @"path/Documents/page3.png",
    @"path/Documents/page2.png"];

If I now sort the array using:

[pathArray sortUsingSelector:@selector(caseInsensitiveCompare:)];

The Array is sorted the wrong way

Result:

pathArray: (
    "path/Documents/page0.png",
    "path/Documents/page1.png",
    "path/Documents/page10.png",
    "path/Documents/page11.png",
    "path/Documents/page12.png",
    "path/Documents/page2.png",
    "path/Documents/page3.png",
    "path/Documents/page4.png",
    "path/Documents/page5.png",
    "path/Documents/page6.png",
    "path/Documents/page7.png",
    "path/Documents/page8.png",
    "path/Documents/page9.png"}

The elements with the components "page10.png", "page11.png" and "page12.png" should be the last three elements. Does another selector exist that covers this issue?

If not how could this be done?

like image 386
pmk Avatar asked Aug 15 '26 11:08

pmk


1 Answers

You can implement your own comparing method using this:

[pathArray sortUsingComparator:^(id one, id two) {
    return [one compare:two options:NSCaseInsensitiveSearch | NSNumericSearch];
}
like image 170
Antonio MG Avatar answered Aug 18 '26 00:08

Antonio MG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!