2013-03-23 41 views
6

对基于密钥使用字典对象排序值的字典值进行排序。其实我正在存储的是,每个字典对象在该字典中具有不同的数据类型,所有的数据类型都以字符串的形式如何将此字符串类型转换为特定的数据类型并对其进行排序,我的代码和输出是波纹管,请帮助我解决这个问题。IOS需要根据关键字价格

-(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; 

}, 
+0

字典中的数据类型有多不同,你可以向他们展示吗? – 2013-03-23 08:07:29

回答

10
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" 
               ascending:YES selector:@selector(localizedStandardCompare:)] ; 

请替换这个并尝试,希望它的作品。

+0

谢谢你krish它的工作 – Vijay 2013-03-24 18:44:14

4
-(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 
} 

希望这有助于。

+0

谢谢kunal – Vijay 2013-03-24 18:45:23