2 ooomiao oOOMIAO 于 2013.03.25 15:13 提问

根据关键字的价格排序

关键字排序问题:

每个dictionary对象有不同的数据类型,全部dictionary都当做string提取了,怎么样将这些string类型的数据转换为不同类型,然后根据价格排序。

-(IBAction)PriceSort:(id)sender
{
    NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"Price" ascending:true] ;
    NSArray *sa = [symbolArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    NSLog(@"price=%@",sa);

}

输出:

{

    volume = 2496752;

    Yield = "10.49";

    MarCap = 829;

    Price = "0.715";

    Symbol = SAIPI;

}

2个回答

redCoral_
redCoral_   2013.03.25 15:58
已采纳
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price"
                                                 ascending:YES selector:@selector(localizedStandardCompare:)] ;
abcdeFGh_xyz
abcdeFGh_xyz   2013.03.25 15:59
-(void)sort
{
    //This is the array of dictionaries, where each dictionary holds a record
    NSMutableArray * array; 
    //allocate the memory to the mutable array and add the records to the arrat

    // I have used simple bubble sort you can use any other algorithm that suites you
    //bubble sort
    //
    for(int i = 0; i < [array count]; i++)
    {
        for(int j = i+1; j < [array count]; j++)
        {
            NSDictionary *recordOne = [array objectAtIndex:i];
            NSDictionary *recordTwo = [array objectAtIndex:j];

            if([[recordOne valueForKey:@"price"] floatValue] > [[recordTwo valueForKey:@"remaining"] floatValue])
            {
                [array exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }   
    }

    //Here you get the sorted array
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!