Je suis nouveau dans le développement de l'iPhone. J'ai besoin de votre aide pour trier NSArray.Comment trier un NSArray par NSSortDiscriptor?
dans ce code J'attribue des valeurs à MyData. c'est une variable NSArray.
NSMutableArray *MyArray=[NSMutableArray array];
[MyArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:314564454],@"Population", @"Mumbai",@"Name", [NSNumber numberWithInt:415452],@"Area",nil]];
[MyArray addObject:[NSDictionary dictionaryWithObjectsAndKeys[NSNumber numberWithInt:454154412],@"Population", @"Chennai",@"Name", [NSNumber numberWithInt:324544],@"Area",nil]];
[MyArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:874544541],@"Population", @"Kolkatta",@"Name", [NSNumber numberWithInt:554445],@"Area",nil]];
[MyArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:412541245],@"Population", @"Delhi",@"Name", [NSNumber numberWithInt:345475],@"Area",nil]];
self.MyData = [NSArray arrayWithArray:MyArray];
Maintenant, j'ai besoin des 3 premières villes avec l'information sur la population la plus élevée.
donc je devais écrire la suite de code.,
NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"Population" ascending:NO] autorelease];
NSArray *sorted = [self.MyData sortedArrayUsingDescriptors:
[NSArray arrayWithObject:sorter]];
NSRange range = NSMakeRange(0,3);
return [sorted subarrayWithRange:range];
Mais il ne fonctionne pas. Le même code fonctionne si je remplace le
suivantNSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"Population" ascending:NO] autorelease];
avec
NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:NO] autorelease];
This Works .. parce que le nom est une chaîne. S'il vous plaît aidez-moi Comment puis-je trier ce tableau avec une valeur numérique.
Merci d'avance.