2012-03-23 6 views
0

リストビューがあり、各名前が正しいUILocalizedIndexCollationセクションの下になるように配列をソートする必要があります。 ここに渡されるセクションのスニペットです。私はnameでソートしたいのですが、オブジェクトをそのままにしておきたいので、他のデータを利用することもできます。パーティションオブジェクトによる照合ソート

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
     NSMutableArray *tempCustomers = [[NSMutableArray alloc] init]; 
     NSString *name; 
     for (NSDictionary *dict in [json objectForKey:@"data"]) { 
      name = [NSString stringWithFormat:@"%@, %@",[dict objectForKey:@"last_name"],[dict objectForKey:@"first_name"]]; 
      NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:dict]; 
      [tempDict setValue:name forKey:@"name"]; 
      [tempCustomers addObject:tempDict]; 

     } 

     self.customers = tempCustomers; 
     self.customerData = [self partitionObjects:[self customers] collationStringSelector:@selector(self)]; 
を呼び出す

2012-03-23 12:10:14.083 MyApp[61241:14803] Section: (
     { 
     "c_id" = 261; 
     "customer_id" = 178664; 
     "first_name" = My; 
     "last_name" = Test; 
     name = "Test, My"; 
    }, 
     { 
     "c_id" = 261; 
     "customer_id" = 185182; 
     "first_name" = valid; 
     "last_name" = Test; 
     name = "Test, valid"; 
    }, 
     { 
     "c_id" = 261; 
     "customer_id" = 178729; 
     "first_name" = Test; 
     "last_name" = Three; 
     name = "Three, Test"; 
    }, 
     { 
     "c_id" = 261; 
     "customer_id" = 178727; 
     "first_name" = Test; 
     "last_name" = Two; 
     name = "Two, Test"; 
    }, 
     { 
     "c_id" = 261; 
     "customer_id" = 178728; 
     "first_name" = Test; 
     "last_name" = Two; 
     name = "Two, Test"; 
    } 
) 

はパーティション

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector 
{ 
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; 
    NSInteger sectionCount = [[collation sectionTitles] count]; 
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount]; 

    for (int i = 0; i < sectionCount; i++) { 
     [unsortedSections addObject:[NSMutableArray array]]; 
    } 

    for (id object in array) { 
     NSInteger index = [collation sectionForObject:[object objectForKey:@"name"] collationStringSelector:selector]; 
     [[unsortedSections objectAtIndex:index] addObject:object]; 
    } 

    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount]; 

    for (NSMutableArray *section in unsortedSections) { 
     [sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]]; 
    } 

    return sections; 
} 

私は無傷ではなく、ソート名前でオブジェクトを保持し、各オブジェクトを持つことが必要としていますがとの適切なセクションに置かれますUILocalizedIndexCollation

ANSWER CODE USED:

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector 
{ 
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; 
    NSInteger sectionCount = [[collation sectionTitles] count]; 
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount]; 

    for (int i = 0; i < sectionCount; i++) { 
     [unsortedSections addObject:[NSMutableArray array]]; 
    } 

    for (id object in array) { 
     NSInteger index = [collation sectionForObject:[object objectForKey:@"name"] collationStringSelector:selector]; 
     [[unsortedSections objectAtIndex:index] addObject:object]; 
    } 

    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount]; 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 


    for (NSMutableArray *section in unsortedSections) { 
     NSArray *sortedArray = [section sortedArrayUsingDescriptors:sortDescriptors]; 
     collationStringSelector:selector]]; 
     [sections addObject:sortedArray]; 
    } 

    return sections; 
} 
+0

これは役に立つかもしれません: http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom- objects-in-it – benuuu

+0

ここにコードを置いてください。 – Shmidt

+0

@Flink何を意味していますか?コードは上に掲載されています。 – Bot

答えて

1

あなたが比較の方法でそれを行うことができるはずです。

その良い例がここに提供されていますhttps://stackoverflow.com/a/805589/580291

+0

これは完璧に機能しました。私は照合の並べ替えでそれを使用する必要があると思ったが、NOPE!ありがとう! – Bot

関連する問題