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

Praful Kadam
  • 280
  • 3
  • 19
  • 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. – Fogmeister Sep 26 '13 at 06:47

2 Answers2

2

Try this:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"type1.size" ascending:YES];
NSArray *finalArray = [self.firstArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
Anil
  • 2,394
  • 2
  • 32
  • 49
  • @PrafulKadam just replace "type1.size" from this answer with "type2.properties.payment". This will sort the array how you want. – Fogmeister Sep 26 '13 at 06:49
  • The questions was edited I think. As @Fogmeister mentioned, change "type1.size" to "type2.properties.payment" – Anil Sep 26 '13 at 07:00
  • -[__NSArrayI compare:]: unrecognized selector sent to instance 0x825f410 – Praful Kadam Sep 26 '13 at 07:23
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;
}
Xcoder
  • 1,747
  • 13
  • 17
  • 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"] <.> – Xcoder Sep 26 '13 at 06:40
  • tried this but it is sorting randomly and not by payments key – Praful Kadam Sep 26 '13 at 06:48
  • I was actually thinking payments as quantity. You will have to specify the kind of sort you need here. – Xcoder Sep 26 '13 at 06:52
  • 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 – Praful Kadam Sep 26 '13 at 06:53
  • You will have to construct NSDate objects from these strings and compare them within the block. See this http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it – Xcoder Sep 26 '13 at 06:59