Help me sort NSMutableArray full of NSDictionary objects - Objective C

I have an NSMutableArray full of NSDictionary objects. Thus

NSMutableArray *names = [[NSMutableArray alloc] init]; for (NSString *string in pathsArray) { NSString *path = [NSString stringWithFormat:@"/usr/etc/%@",string]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:string,@"name",path,@"path",nil]; } 

pathsArray is not sorted, so I am stuck with the order of objects inside it. I would like to sort the array of names in alphabetical order of objects for the key: @ "name" in the dictionary. Can this be done easily or will several levels of enumeration be required?

EDIT: I found the answer to SO in this question: NSMutableArray Sort

Class NSSortDescriptor.

 NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [names sortUsingDescriptors:[NSArray arrayWithObject:sortName]]; [sortName release]; 

Anyone want free answers?

+6
source share
1 answer

Try something like this:

  NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSArray *sortedArray = [names sortedArrayUsingDescriptors:sortDescriptors]; // names : the same name of the array you provided in your question. 
+18
source

Source: https://habr.com/ru/post/886546/


All Articles