帮我排序NSMutableArray充满NSDictionary对象 - Objective C

Jus*_*tin 6 objective-c

我有一个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];
}
Run Code Online (Sandbox Code Playgroud)

pathsArray是不可排序的,所以我坚持它里面的对象的顺序.我想按键的对象的字母顺序对名称数组进行排序:字典中的@"name".这可以轻松完成还是需要几个级别的枚举?

编辑:我在这个问题上找到了答案:排序NSMutableArray

NSSortDescriptor类.

NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[names sortUsingDescriptors:[NSArray arrayWithObject:sortName]];
[sortName release];
Run Code Online (Sandbox Code Playgroud)

有人想要获得一些免费的答案点吗?

Ahm*_*ali 18

尝试这样的事情:

 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.
Run Code Online (Sandbox Code Playgroud)