9

I have a array with 10 elements called products which is sorted by default, this is the current log now.

for (int i=0;i<products.count; i++)
{
     NSLog(@"%@",products[i]);
}

The Output:

Product1
Product10
Product2
Product3
Product4
Product5
Product6
Product7
Product8
Product9

I need to sort it in following order:

Product1
Product2
Product3
Product4
Product5
Product6
Product7
Product8
Product9
Product10

My current method is to scan out the numbers and sort based on that, I was wondering if there is any other way or and default method in iOS that does this or should I have to stick with my current method of scanning the numbers in each element and then sort??

1
  • May be you can change the Product Modul to include an int property, than when create the product object, setup the int value. After that just use this value to sort.
    – JerryZhou
    Dec 24, 2013 at 9:48

2 Answers 2

17

You can use this code to sort array. Use NSNumericSearch to search the numeric value in string.

NSArray * products = [[NSArray alloc] initWithObjects:@"Product1",
                                                      @"Product10",
                                                      @"Product2",
                                                      @"Product3",
                                                      @"Product4",
                                                      @"Product5",
                                                      @"Product6",
                                                      @"Product7",
                                                      @"Product8",
                                                      @"Product9",
                                                      nil];

products = [products sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
    }];

NSLog(@"products : %@", products);

And the log display :

products : (
    Product1,
    Product2,
    Product3,
    Product4,
    Product5,
    Product6,
    Product7,
    Product8,
    Product9,
    Product10
)
3
  • 1
    this is called smart answer..thanks
    – user2299177
    Dec 11, 2015 at 7:16
  • @i_Looser thank you... :) Dec 11, 2015 at 16:12
  • Using NSNumericSearch | NSCaseInsensitiveSearch ought to ensure capitalization doesn't mess up the sort too
    – beebcon
    Nov 10, 2017 at 20:21
-1
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc]initWithKey:@"YourKeyName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];


self.products = [[self.products sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDesc, nil]] mutableCopy];

For more details Apple Documentation

4
  • 1
    This is incorrect. have you checked your code?
    – user2452016
    Dec 24, 2013 at 11:11
  • 1
    Returns products === ( Product1, Product10, Product2, Product3, Product4, Product5, Product6, Product7, Product8, Product9 )
    – user2452016
    Dec 24, 2013 at 11:11
  • Yes I have checked before posting my answer.
    – Mehmood
    Dec 24, 2013 at 11:15
  • 1
    And at that time its returns correct values? Wonderss
    – user2452016
    Dec 24, 2013 at 11:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.