2011-04-23 2 views
6

NSMutableArray에 NSDictionary 객체가 가득 찼습니다. 좋아요NSMutableArray NSDictionary 객체로 전체를 정렬하도록 도와주세요 - Objective C

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"키의 객체를 사전 순으로 정렬하려고합니다. 이 작업을 쉽게 완료 할 수 있습니까? 아니면 몇 가지 수준의 열거 형을 사용합니까?

편집 : Sort NSMutableArray

NSSortDescriptor 클래스 :이 질문에 SO에 대한 답을 찾을.

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

무료 응답 점이 있으십니까? 이 같은

답변

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. 
+0

감사합니다! 멋진 질문에 대한 upvote 케어? =) – Justin

+0

변수를 즉시 할당하지 않고 이상하게 만든 변수가있는 이유는 무엇입니까? –

+0

@Jamie Chapman : 지금 어떻게 보이나요? –

관련 문제