2013-03-23 1 views
6

'm konfrontiert Problem, um die Werte basierend auf Schlüssel mit Dictionary-Objekt zu sortieren. Eigentlich was ich speichere, ist, dass jedes Wörterbuchobjekt einen anderen Datentyp in diesem Wörterbuch hat, wobei der ganze Datentyp als eine Zeichenkette nimmt, wie man diesen Zeichenkettentyp in einen spezifischen Datentyp umwandelt und es price vise sortiert, mein Code und output ist, bitte Hilf mir dabei.IOS müssen ein Array von Wörterbüchern Wert basierend auf Schlüsselpreis sortieren

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

} 

löschte {

volume = 2496752; 

    Yield = "10.49"; 

    MarCap = 829; 

    Price = "0.715"; 

    Symbol = SAIPI; 

}, 
+0

Wie verschiedene Datentypen im Wörterbuch, können Sie sie zeigen? –

Antwort

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

Bitte ersetzen Sie dieses und versuchen Sie, hoffe, es funktioniert.

+0

danke krish seine Arbeit – Vijay

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 
} 

Hoffnung, das hilft.

+0

Danke kunal – Vijay

Verwandte Themen