2011-01-18 7 views

Répondre

88

Vous pouvez le faire pour trier NSMutableArray:

[yourArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
+0

[tempPeoples sortUsingSelector: @selector (localizedCaseInsensitiveCompare :)]; J'ai utilisé ce code, il donne l'exception – Ali

+0

pouvez-vous poster la déclaration d'exception. –

+0

NAddressBook [5197: 207] *** - [AddressData localizedCaseInsensitiveCompare:]: sélecteur non reconnu envoyé à l'instance 0x149160 2011-01-18 17: 01: 14.470 NAddressBook [5197: 207] *** Fin de l'application en raison d'une exception non interceptée ' NSInvalidArgumentException ', raison:' *** - [AddressData localizedCaseInsensitiveCompare:]: sélecteur non reconnu envoyé à l'instance 0x149160 ' 2011-01-18 17: 01: 14.484 NAddressBook [5197: 207] Pile: ( – Ali

0

Utilisez la classe NSSortDescriptor et de repos, vous obtiendrez tout here

46

Les autres réponses fournies mentionner ici en utilisant @selector (localizedCaseInsensitiveCompare :)
Cela fonctionne très bien pour un tableau de NSString, cependant l'OP a commenté que le tableau contient des objets et que le tri doit être fait en fonction de la propriété object.name.
Dans ce cas, vous devez faire ceci:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:sort]]; 

Vos objets seront triés en fonction de la propriété du nom de ces objets.

+0

Exactement ce dont j'avais besoin! Merci! – RanLearns

+1

Ceci est la meilleure réponse à cette question spécifique. Fonctionne très bien! –

+0

ce que je veux. +1 – pkc456

1
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; // Describe the Key value using which you want to sort. 
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor]; // Add the value of the descriptor to array. 
sortedArrayWithName = [yourDataArray sortedArrayUsingDescriptors:descriptors]; // Now Sort the Array using descriptor. 

Ici, vous obtiendrez la liste de tableaux triés.

0
NSSortDescriptor * sortDescriptor; 
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name_your_key_value" ascending:YES]; 
NSArray * sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
NSArray * sortedArray; 
sortedArray = [Your_array sortedArrayUsingDescriptors:sortDescriptors]; 
0

Peut-être que cela peut vous aider:

[myNSMutableArray sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES],[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]]]; 

Tout est acording à NSSortDescriptor ...

Questions connexes