0

我的数组包含每个数组对象的三个字典。

{
    avg = {
        avg1 = 50;
        avg2 = 60;
    };

    posts =         {
        alcoholContent = 450;
        name = "BBB";
        origin = United States;

    };

    reviews =  {

        rev1 = "Test review 1";
        rev2 = "Test review 2";
    };
}

{
    avg = {
        avg1 = 30;
        avg2 = 20;
    };

    posts =         {
        alcoholContent = 550;
        name = "AAA";
        origin = United States;

    };

    reviews =  {

        rev1 = "Test review 1";
        rev2 = "Test review 2";
    };
}

我想通过键“名称”(帖子字典)对数组进行排序。我该怎么做?

我尝试了使用排序描述符的正常排序方法,但没有奏效

4

1 回答 1

1

尝试 sortUsingComparator:

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSDictionary *dict1 = obj1;
    NSDictionary *dict2 = obj2;

    NSString *string1 = [[dict1 objectForKey:@"posts"] objectForKey:@"name"];
    NSString *string2 = [[dict2 objectForKey:@"posts"] objectForKey:@"name"];

    return [string1 compare:string2];
}];
于 2013-01-07T09:34:42.150 回答