How can I sort an NSArray containing NSDictionaries?

I have NSArrayone that contains objects NSDictionary. Each of these NSDictionaryobjects contains a key ORDER.

How can I sort this NSArraybased on this key in each of these NSDictionaries?

+1
source share
3 answers

Here is an example. Imagine you have an array of dictionaries. In the text of "Ecah dictionnary" there are 2 keys "name" and "dateOfBirth". You can sort the array using "name" as follows:

//
// Sort array by name
//
NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];
[Sorter release];

Please note that myArrayis NSMutableArray.

+7
source

.

.

+4

NSSortDescriptor

, , NSMutableArray dict Object , .

-(NSMutableArray*)sortingArrayOfDict:(NSMutableArray *)arrayTosort sortByObject:(NSString*)sortKey{
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:YES];
[arrayTosort sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
return arrayTosort;
 }

, YES/NO

+1

All Articles