나름 프로그래밍?/ Objective-C

NSSortDescriptor : Array 에 담긴 Dictionary 의 값을 이용해 소팅

-Dong- 2010. 6. 17. 03:48
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"22", @"published",@"33",@"key",nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"23", @"published",@"34",@"key",nil];
    NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"22", @"published",@"35",@"key",nil];
    NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"24", @"published",@"36",@"key",nil];
    NSDictionary *dic5 = [NSDictionary dictionaryWithObjectsAndKeys:@"21", @"published",@"37",@"key",nil];
   
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    [array addObject:dic];
    [array addObject:dic2];
    [array addObject:dic3];
    [array addObject:dic4];
    [array addObject:dic5];
   
    NSSortDescriptor *publishedSorter =     [[[NSSortDescriptor alloc] initWithKey:@"published"
                                                                        ascending:YES                                             
                                                                         selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
    NSLog(@"%@", [array description]);
    [array sortUsingDescriptors:[NSArray arrayWithObject:publishedSorter]];
   
    NSLog(@"%@", [array description]);