简体   繁体   English

排序NSMutableArray的问题

[英]Issue with Sorting NSMutableArray

I have an NSMutableArray which contains an NSDictionary, I need to sort the array based on certain criteria. 我有一个包含NSDictionary的NSMutableArray,我需要根据某些条件对数组进行排序。

Sorting criteria is 排序标准是

  1. sort based on properties.property.value , suppose if I want to sort based on properties.property.name = Risk value. 基于properties.property.value排序,假设我想基于properties.property.name =风险值进行排序。 I want all the elements with Risk value first with the value in Ascending order. 我希望所有具有Risk值的元素首先使用升序中的值。 That means for the Dictionary which doesn't have properties.property.name = Risk must come last. 这意味着对于没有properties.property.name = Risk的词典必须是最后的。 Some of the dictionaries don't have these names. 有些词典没有这些名字。

Please help... 请帮忙...

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

Try with this 试试这个

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

}];

It sounds like you have multiple different requirements (logic) which go into determining the desired sort result. 听起来你有多种不同的要求(逻辑)来确定所需的排序结果。 You should write your own comparison logic and use one of the array methods like sortUsingComparator: . 您应该编写自己的比较逻辑并使用其中一个数组方法,如sortUsingComparator: .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM