1

我有一个嵌套数组,想通过内部数组中存在的键对其进行排序。下面给出的是我想使用NSSortDescriptor或以其他方式排序的数组。

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

如何使用键“type2->properties->payment”对数组进行排序?

- - - -更新 - - - - - -

我修改了数组并使用了 NSSortDescriptor 解决了我的问题

4

2 回答 2

2

尝试这个:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"type1.size" ascending:YES];
NSArray *finalArray = [self.firstArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
于 2013-09-26T06:06:03.013 回答
0

通过将票价数组输入此方法来尝试此操作 -

+(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;
}
于 2013-09-26T06:28:47.537 回答