1

I have a nested array and want to sort it by a key present inside the inner array. below given is my array which I want to sort using NSSortDescriptor or in other way.

fares(
{
    pid = 1;
    type1 = (
    {
        color = "red";
        size = "big";
        properties = (
        {
            mod = "auto";
            payment = "EMI";
            moresegs = (
            {
                id = 141;
                name = "abcd";
                duration = "1 year"
            })
        })
    });
    type2 = (
    {
        color = "green";
        size = "small";
        properties = (
        {
            mod = "auto";
            payment = "EMI";
            moresegs = (
            {
                id = 141;
                name = "abcd";
                duration = "1 year"
            })
        })
    })
}

{
    pid = 1;
    type1 = (
    {
        color = "red";
        size = "big";
        properties = (
        {
            mod = "auto";
            payment = "EMI";
            moresegs = (
            {
                id = 141;
                name = "abcd";
                duration = "1 year"
            })
        })
    });
    type2 = (
    {
        color = "green";
        size = "big";
        properties = (
        {
            mod = "auto";
            payment = "EMI";
            moresegs = (
            {
                id = 141;
                name = "abcd";
                duration = "1 year"
            })
        })
    })
})

How can i sort above array using key "type2->properties->payment"?

-------update-----------

I modified the array and used NSSortDescriptor which solved my problem

1
  • Please format your code. Unlike computers, us humans can't read random splurges of text. It isn't hard to format correctly. Just do it. Commented Sep 26, 2013 at 6:47

2 Answers 2

2

Try this:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"type1.size" ascending:YES];
NSArray *finalArray = [self.firstArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
Sign up to request clarification or add additional context in comments.

3 Comments

@PrafulKadam just replace "type1.size" from this answer with "type2.properties.payment". This will sort the array how you want.
The questions was edited I think. As @Fogmeister mentioned, change "type1.size" to "type2.properties.payment"
-[__NSArrayI compare:]: unrecognized selector sent to instance 0x825f410
0

Try this by inputing the fares array into this method-

+(NSArray*)sortedListBySize:(NSArray*)_unsortedList{
    NSArray *sortedListBySize = [_unsortedList sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2){

        if ([[obj1 valueForKeyPath:@"type2.properties.payments" ] intValue] < [[obj1 valueForKeyPath:@"type2.properties.payments"] intValue]) {
            return NSOrderedAscending;
        } else if ([[obj1 valueForKeyPath:@"type2.properties.payments" ] intValue] > [[obj1 valueForKeyPath:@"type2.properties.payments"] intValue]) {
            return NSOrderedDescending;
        }
        return NSOrderedSame;
    }];
    return sortedListBySize;
}

5 Comments

payments is not an intvalue-> that was my assumption. Thats why I wrote [payment(NSString) intValue]. You need not use it then- just use [obj1 valueForKeyPath:@"type2.properties.payments"] <.
tried this but it is sorting randomly and not by payments key
I was actually thinking payments as quantity. You will have to specify the kind of sort you need here.
ok suppose it is duration like "10:00", "02:30",..... in this case how can i sort can you please explain. I am trying to solve this since long time
You will have to construct NSDate objects from these strings and compare them within the block. See this stackoverflow.com/questions/805547/…

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.