排序描述符不适用于ios

时间:2015-07-11 09:05:51

标签: ios sorting nssortdescriptor

我使用排序描述符对NSMutableArray进行排序一个&多个值,首先我尝试按By Price排序,它在其他一些订单中排序这里是我的代码帮助我, 我的 我通过下面的代码创建字典并添加到NSMutableArray

    for(int i=0;i<[priceArray count];i++)
    {
        cellDict=[[NSMutableDictionary alloc]init];
        [cellDict setObject:nameArray[i] forKey:@"Name"];
        [cellDict setObject:splPriceArray[i] forKey:@"Percentage"];
        [cellDict setObject:priceArray[i] forKey:@"Price"];
        [resultArray addObject:cellDict];
    }
// To Sort in Ascending Order
    NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Price" ascending:YES];
    NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
    NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];

    NSLog(@"Result %@ Sorted arr %@",resultArray, sortedArray);

输出是:

 Result (
        {
        Name = "Black Eyed Peas";
        Percentage = 0;
        Price = 80;
    },
        {
        Name = "Black Gram";
        Percentage = 0;
        Price = 56;
    },
        {
        Name = "Channa White";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Double Beans";
        Percentage = 0;
        Price = 95;
    },
        {
        Name = "Gram Dall";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Green Moong Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Ground Nut";
        Percentage = 0;
        Price = 140;
    },
        {
        Name = "Moong Dal";
        Percentage = 0;
        Price = 75;
    },
        {
        Name = "Orid Dal";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Toor Dal";
        Percentage = 0;
        Price = 150;
    }
) Sorted arr (
        {
        Name = "Channa White";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Gram Dall";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Orid Dal";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Ground Nut";
        Percentage = 0;
        Price = 140;
    },
        {
        Name = "Green Moong Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Toor Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Black Gram";
        Percentage = 0;
        Price = 56;
    },
        {
        Name = "Moong Dal";
        Percentage = 0;
        Price = 75;
    },
        {
        Name = "Black Eyed Peas";
        Percentage = 0;
        Price = 80;
    },
        {
        Name = "Double Beans";
        Percentage = 0;
        Price = 95;
    }
)

此处排序数组按其他订单排序我想按价格按升序排序。

1 个答案:

答案 0 :(得分:1)

目前还不清楚您的测试数据是什么样的 - 但以下代码段按预期工作

NSArray *priceArray = [NSArray arrayWithObjects:@(74),@(100),@(100),@(130), nil];
    NSArray *nameArray = [NSArray arrayWithObjects:@"Yva",@"Hallo", @"Adam", @"Xavier", nil];

NSMutableArray *resultArray = [NSMutableArray new];
for(int i=0;i<[priceArray count];i++)
{
    NSMutableDictionary *cellDict=[[NSMutableDictionary alloc]init];
    [cellDict setObject:nameArray[i] forKey:@"Name"];
    [cellDict setObject:priceArray[i] forKey:@"Percentage"];
    [cellDict setObject:priceArray[i] forKey:@"Price"];
    [resultArray addObject:cellDict];
}
// Sort by Name
//NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Price" ascending:YES];

// Sort by Name
NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];

NSLog(@"Result %@ Sorted arr %@",resultArray, sortedArray);

结果:

2015-07-11 12:54:54.358 ret[10480:162783] Result (
        {
        Name = Yva;
        Percentage = 74;
        Price = 74;
    },
        {
        Name = Hallo;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Adam;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Xavier;
        Percentage = 130;
        Price = 130;
    }
) Sorted arr (
        {
        Name = Adam;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Hallo;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Xavier;
        Percentage = 130;
        Price = 130;
    },
        {
        Name = Yva;
        Percentage = 74;
        Price = 74;
    }
)