我有一个NSMutableArray充满了NSDictionary对象.像这样
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是不可排序的,所以我坚持它里面的对象的顺序.我想按键的对象的字母顺序对名称数组进行排序:字典中的@“name”.这可以轻松完成还是需要几个级别的枚举?
编辑:我在这个问题上找到了关于SO的答案:Sort NSMutableArray
NSSortDescriptor类.
NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [names sortUsingDescriptors:[NSArray arrayWithObject:sortName]]; [sortName release];
有人想要获得一些免费的答案点吗?
解决方法
尝试这样的事情:
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.