꼼시월드

중첩된 딕셔너리의 키를 이용하여 정렬하기 본문

iOS

중첩된 딕셔너리의 키를 이용하여 정렬하기

꼼시 2014. 1. 24. 15:50

소스코드


 NSArray *arr = @[
  @{@"name": @"kim", @"profile": @{@"type": @1} },
  @{@"name": @"jung", @"profile": @{@"type": @1} },
  @{@"name": @"park", @"profile": @{@"type": @2} },
  @{@"name": @"hong", @"profile": @{@"type": @2} },
  ];
    
    //ascending:YES 오름차순 :NO 내림차순
    NSSortDescriptor *typeDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"profile.type" ascending:NO];

    NSArray *sortedArray = [arr sortedArrayUsingDescriptors:@[typeDescriptor]];
    
    NSLog(@"sortedArray = %@",sortedArray);


profile 딕셔너리 안에 type 딕셔너리가 중첩된 형태인 경우 중첩된 딕셔너리 키를 이용하여 배열을 정렬하고자 하는 경우 프로퍼티 접근하는것처럼 . 을 이용하여 정렬을 하면 된다. [NSSortDescriptor sortDescriptorWithKey:@"profile.type"]; 


profile 안의 type으로 내림차순 정렬을 수행하였다.


결과 


Comments