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

后端 未结 4 1586
星月不相逢
星月不相逢 2020-12-07 06:28

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 ,butto

相关标签:
4条回答
  • 2020-12-07 06:54

    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
    
    0 讨论(0)
  • 2020-12-07 07:09

    Use the NSNumericSearch option in your sort comparison.

    0 讨论(0)
  • 2020-12-07 07:10

    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"
    )
    
    0 讨论(0)
  • 2020-12-07 07:15

    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

    0 讨论(0)
提交回复
热议问题