Tutorial :How to add these object with name to the array by this order?



Question:

I have some object with name , just like button 1 , button 2 , button 11 ,button 111 , I want to add this object to a array by this order: button 1 ,button 2 , button 11 , button 111 ,but I found that these object can add the array by this order: button 1 , button 11,button 111 , button 2.why ? Thank you for your answer!


Solution:1

Use the NSNumericSearch option in your sort comparison.


Solution:2

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

NSArray * ary = [[NSArray alloc] initWithObjects:@"button 1" , @"button 2" , @"button 11" ,@"button 111", nil];    ary = [ary sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {          return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];      }];    NSLog(@"ary : %@", ary);  

And the log display :

ary : (  "button 1",  "button 2",  "button 11",  "button 111"  )  


Solution:3

I have used the NSNumericSearch|NSCaseInsensitiveSearch combination to get the best result. I hope somebody will save their lifetime in googeling. Check the results.

    NSArray * array =      [NSArray       arrayWithObjects:          [NSDictionary dictionaryWithObject: @"C3" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"C1" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"C100" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"Z0" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"Z1000" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"M" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"m" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"M" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"m" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"1A4" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"A2" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"A12" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"B4" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"B6" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"B100" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"B1" forKey: @"sort"],          [NSDictionary dictionaryWithObject: @"1a4" forKey: @"sort"],           nil];        NSSortDescriptor * descriptor =      [NSSortDescriptor       sortDescriptorWithKey: @"sort"       ascending: YES       comparator:       ^NSComparisonResult(id obj1, id obj2)       {           return [obj1 compare: obj2 options: NSNumericSearch|NSCaseInsensitiveSearch];       }];        NSArray * sortedArray =      [array       sortedArrayUsingDescriptors:       [NSArray arrayWithObject: descriptor]];    NSLog(@"Original array:");      for(NSDictionary * value in array)      NSLog(@"%@", [value objectForKey: @"sort"]);    NSLog(@"Sorted array:");    for(NSDictionary * value in sortedArray)      NSLog(@"%@", [value objectForKey: @"sort"]);  

Result

Original array:

    C3      C1      C100      Z0      Z1000      M      m      M      m      1A4      A2      A12      B4      B6      B100      B1      1a4  

Sorted array:

    1A4      1a4      A2      A12      B1      B4      B6      B100      C1      C3      C100      M      m      M      m      Z0      Z1000  


Solution:4

The same way that if you had files of this name in Finder they would order the same way.

To get proper ordering you need to name them button 001, button 002, button 011, etc.

Alternatively, you'll have to write your own sort descriptors.

See here for NSMutableArray sortUsingSelector: and here for NSSortDescriptor


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »