如何在ios中使用字典对数组的顺序进行排序

时间:2014-05-20 13:40:03

标签: ios sorting nsarray nsdictionary alphabetical

我有这样一个阵列。

rows1 (
    "Adam Harris",
    "Devraj Singh",
    "Devraj Singh",
    "Er Devraj Gurjar",
    "Ghh HHS",
    "Mark Json",
    "Ninehertz India",
    "Ninehertz India",
    "Test User",
    "Test Test",
    "Yatin TFT"
)

我使用了以下代码

NSArray *rows = ...;
    NSMutableDictionary *map = [NSMutableDictionary dictionary];
    for (NSString *value in rows) {
        NSString *firstLetter = [value substringToIndex:1];
        if (!map[firstLetter]) {
            map[firstLetter] = @[];
        }
        NSMutableArray *values = [map[firstLetter] mutableCopy];
        [values addObject:value];
        map[firstLetter] = values;
    }
    NSArray *finalRows = [map allValues];

按字母顺序对数组进行排序。在排序1中将存在两个条件。)所有以相同字母开头的单词应该被分组,如下面的数组所示。现在我的问题是我想按字母顺序排列所有分组的项目。 2.)现在,数组的所有分组元素应按字母顺序排序。以下是我所取得的成就。

 (
            (
            "Devraj Singh",
            "Devraj Singh"
        ),
            (
            "Mark Json"
        ),
            (
            "Er Devraj Gurjar"
        ),
            (
            "Adam Harris"
        ),
            (
            "Ninehertz India",
            "Ninehertz India"
        ),
            (
            "Test User",
            "Test Test"
        ),
            (
            "Ghh HHS"
        ),
            (
            "Yatin TFT"
        )
    )

我想做的是如下

(
                (
                "A1",
                "A2"
            ),
                (
                "B1"
            ),
                (
                "C1"
            ),
                (
                "D1"
            ),
                (
                "E1",
                "E2",
                "E3"
            ),
                (
                "F1",
                "F2"
            ),
                (
                "G1"
            ),
                (
                "H1"
            )
        )

2 个答案:

答案 0 :(得分:3)

最简单的解决方案是将最后一行更改为:

NSArray *finalRows = [[map allValues] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSString *letter1 = [obj1[0] substringToIndex:1];
        NSString *letter2 = [obj2[0] substringToIndex:1];
        return [letter1 compare:letter2];
    }];

它不是最有效的解决方案,但它确实有效。

答案 1 :(得分:0)

你可以实现以下目标:

NSArray *rows1 = @[
                       @"Adam Harris",
                       @"Devraj Singh",
                       @"Devraj Singh",
                       @"Er Devraj Gurjar",
                       @"Ghh HHS",
                       @"Mark Json",
                       @"Ninehertz India",
                       @"Ninehertz India",
                       @"Test User",
                       @"Test Test",
                       @"Yatin TFT"
                       ];

    NSArray *sortedArray = [rows1 sortedArrayUsingSelector:@selector(compare:)];

    NSMutableDictionary *dict = [NSMutableDictionary new];

    //make keys and store
    for (NSString *str in sortedArray) {
        NSString *key = [str substringToIndex:1];

        //store the str if this is first object
        if (dict[key]== nil ) {
            NSArray *arr = @[str];
            [dict setObject:arr forKey:key];
        }
        //if there is another get the array and add more
        else{
            NSMutableArray *arr = [dict[key] mutableCopy];
            [arr addObject:str];
            [dict setObject:arr forKey:key];
        }

    }
    NSLog(@"Dict: %@",dict);