关键字排序问题:
每个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;
}
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price"
ascending:YES selector:@selector(localizedStandardCompare:)] ;
-(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
}