0

NSDictionary を含む NSMutableArray があります。特定の基準に基づいて配列をソートする必要があります。

ソート基準は

  1. に基づいて並べ替え、プロパティproperties.property.valueに基づいて並べ替えたいとします。プロパティ名 = リスク値。リスク値を持つすべての要素が最初に昇順で値を持つようにします。つまり、持っていない Dictionary は properties.property.name = Risk最後に来る必要があります。一部の辞書にはこれらの名前がありません。

助けてください...

finalArr (
    {
    ViewTag = 101;
    "action-taken-date" =         {
        class = "java.util.GregorianCalendar";
        text = "2013-06-03 22:23:28.0 PDT";
    };
    alarms = 0;
    alerts = 0;
    application =         {
    assignee = "Test Name";
    completed = false;
    "properties" =         {
        class = "java.util.ArrayList";
        property =             (
                            {
                label = Risk;
                name = Risk;
                value = "3 - Low";
            },
                            {
                label = "Start Time";
                name = "Start Time";
                value = "05/09/13 06:00:00";
            }
        );
    };

    {
    ViewTag = 102;
    "date" =         {
        class = "java.util.GregorianCalendar";
        text = "2013-06-03 22:23:28.0 PDT";
    };
    alarms = 0;
    alerts = 0;
    application =         {
    assignee = "Test Name";
    completed = false;
    "properties" =         {
        class = "java.util.ArrayList";
        property =             (
                            {
                label = Risk;
                name = Risk;
                value = "2 - Low";
            },
                            {
                label = "Start Time";
                name = "Start Time";
                value = "05/09/13 06:00:00";
            }
        );
    };

    {
    ViewTag = 103;
    "date" =         {
        class = "java.util.GregorianCalendar";
        text = "2013-06-03 22:23:28.0 PDT";
    };
    alarms = 0;
    alerts = 0;
    application =         {
    assignee = "Test Name";
    completed = false;
    "properties" =         {
        class = "java.util.ArrayList";
        property =             (
                            {
                label = Status;
                name = Status;
                value = "Pending Signoffs";
            },
                            {
                label = Priority;
                name = Priority;
                value = 3;
            }
        );
    };

{
    ViewTag = 104;
    "date" =         {
        class = "java.util.GregorianCalendar";
        text = "2013-06-03 22:23:28.0 PDT";
    };
    alarms = 0;
    alerts = 0;
    application =         {
    assignee = "Test Name";
    completed = false;
    "properties" =         {
        property =             (
            {
                label = Priority;
                name = priority;
                value = 1;
            },
            {
                label = "Start Time";
                name = "Start Time";
                value = "05/09/13 06:00:00";
            }
        );
    };

{
    ViewTag = 103;
    "date" =         {
        class = "java.util.GregorianCalendar";
        text = "2013-06-03 22:23:28.0 PDT";
    };
    alarms = 0;
    alerts = 0;
    application =         {
    assignee = "Test Name";
    completed = false;
    "properties" =         {
        property =             (
                            {
                label = Status;
                name = Status;
                value = "Pending Signoffs";
            },
                            {
                label = Priority;
                name = Priority;
                value = 2;
            }
        );
    };
)
4

2 に答える 2

2

これで試してください

NSString *risk = @"Risk";
[finalArray sortUsingComparator:^NSComparisonResult(NSDictionary * dict1, NSDictionary * dict2) {

    NSDictionary *property1 = dict1[@"properties"][@"property"][0];
    NSString *name1 = property1[@"name"];

    NSDictionary *property2 = dict2[@"properties"][@"property"][0];
    NSString *name2 = property2[@"name"];

    if ([name1 isEqualToString:name2]) {
        //if both names are equal then they are sorted based on value
        NSString *value1 = property1[@"value"];
        NSString *value2 = property2[@"value"];
        return [value1 compare:value2 options:NSNumericSearch];
    }else if ([name1 isEqualToString:risk]){
        //if name1 is equal to risk it is moved up
        return NSOrderedAscending;
    }else{
        return NSOrderedDescending;
    }

}];
于 2013-06-08T09:37:08.430 に答える
0

目的のソート結果を決定するための複数の異なる要件 (ロジック) があるようです。独自の比較ロジックを作成し、 のような配列メソッドのいずれかを使用する必要がありますsortUsingComparator:

于 2013-06-08T09:36:01.070 に答える