0

I have an NSMutableArray which contains an NSDictionary, I need to sort the array based on certain criteria.

Sorting criteria is

  1. sort based on properties.property.value, suppose if I want to sort based on properties.property.name = Risk value. I want all the elements with Risk value first with the value in Ascending order. That means for the Dictionary which doesn't have properties.property.name = Risk must come last. 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;
            }
        );
    };
)
1
  • 2
    When you are finding it difficult to work with, there is something wrong with the design. Commented Jun 8, 2013 at 9:35

2 Answers 2

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

}];
Sign up to request clarification or add additional context in comments.

Comments

0

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:.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.