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

时间:2015-02-09 12:48:01

标签: ios sorting nsmutablearray nsmutabledictionary

我的数组包含如下字典:

(
{  
    "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; 

        }
        )

1 个答案:

答案 0 :(得分:1)

这就是我想你想要的。我还给了一个样本进行测试。

重点是你在街区内做的事情。

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);

这是你的“特殊”方法:

-(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" = "";
    } )
相关问题