以字母数字方式对包含字典的 nsmutablearray 进行排序

sorting nsmutablearray containing dictionary in alphanumeric way

我有一个包含如下字典的数组:

(
{  
    "act_priority" = B1; 
}
{
    "act_priority" = ""; 

}
{
    "act_priority" = A; 

}
{
    "act_priority" = A3; 

}
{
    "act_priority" = B; 

}
{
    "act_priority" = A2; 

}
{
    "act_priority" = A1; 

}
{
    "act_priority" = ""; 

}
)

我想同时按字母和数字排序:

(
    {  
        "act_priority" = A1; 
    }
    {
        "act_priority" = A2; 

    }
    {
        "act_priority" = A3; 

    }
    {
        "act_priority" = B1; 

    }
    {
        "act_priority" = A; 

    }
    {
        "act_priority" = B; 

    }
    {
        "act_priority" = ""; 

    }
    {
        "act_priority" = ""; 

    }
    )

我试过的如下:

  NSArray* sorted = [activityArray sortedArrayUsingComparator:(NSComparator)^(NSDictionary *item1, NSDictionary *item2) {
            NSString *score1 = [item1 objectForKey:@"act_priority"];
            NSString *score2 = [item2 objectForKey:@"act_priority"];
            return [score1 compare:score2 options:NSNumericSearch];
        }];

还有:

  NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"act_priority" ascending:YES];
        [activityArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];

但它让我喜欢

(

        {  
            "act_priority" = ""; 

        }
        {
            "act_priority" = ""; 

        }
        {
            "act_priority" = A; 

        }
        {
            "act_priority" = B; 

        }
        {
            "act_priority" = A1; 

        }
        {
            "act_priority" = A2; 

        }
        {
            "act_priority" = A3; 

        }
        {
            "act_priority" = B1; 

        }
        )

这就是我认为您想要的。我也给了一个样品来测试。

要点是你在块内做什么。

NSArray *array = @[@{@"act_priority":@"B1"},
                   @{@"act_priority":@""},
                   @{@"act_priority":@"A"},
                   @{@"act_priority":@"A3"},
                   @{@"act_priority":@"B"},
                   @{@"act_priority":@"A2"},
                   @{@"act_priority":@"A1"},
                   @{@"act_priority":@""}];

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2)
{
    NSString *string1 = [obj1 objectForKey:@"act_priority"];
    NSString *string2 = [obj2 objectForKey:@"act_priority"];
    if ([string1 length] == 0)          //To put the @"" at the end
        return NSOrderedDescending;
    else if ([string2 length] == 0)     //To put the @"" at the end
        return NSOrderedAscending;
    else
    {

        BOOL string1HasSuffixNumber = [self hasSuffixNumber:string1];    //If string1 has a number at the end
        BOOL string2HasSuffixNumber = [self hasSuffixNumber:string2];   //If string2 has a number at the end
        if (string1HasSuffixNumber && !string2HasSuffixNumber)
            return NSOrderedAscending; //Put the string2 at the end
        else if (!string1HasSuffixNumber && string2HasSuffixNumber)
            return NSOrderedDescending; //Put the string1 at the end
        else
            return [string1 compare:string2 options:NSCaseInsensitiveSearch]; //Other option can be used, if you want case sensitive one for example, or not, etc.
    }
}];

NSLog(@"SortedArray: %@", sortedArray);

有了这个你的 "special" 方法:

-(BOOL)hasSuffixNumber:(NSString *)string
{
    if ([string length] < 1) 
        return FALSE; //"Security added, but in your use case you shouldn't go there;

    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; //Explicit the character set in case you want to add some changes
    if ([[string substringFromIndex:[string length]-1] rangeOfCharacterFromSet:set].location != NSNotFound)
        return TRUE;
    else
        return FALSE;
}

输出:

>SortedArray: (
        {
        "act_priority" = A1;
    },
        {
        "act_priority" = A2;
    },
        {
        "act_priority" = A3;
    },
        {
        "act_priority" = B1;
    },
        {
        "act_priority" = A;
    },
        {
        "act_priority" = B;
    },
        {
        "act_priority" = "";
    },
        {
        "act_priority" = "";
    } )