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

后端 未结 4 1592
星月不相逢
星月不相逢 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
    

提交回复
热议问题