2012-03-23 124 views
0

我有一个列表视图,并希望对数组进行排序,以便每个名称都在正确的UILocalizedIndexCollation部分下。 以下是正在传入的部分片段。我想通过name进行排序,但我也希望保持对象完整无缺,以便可以使用其他数据。对象排序与对象

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; 
} 

调用分区

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)]; 

我需要保持对象完好,但按名称排序,并让每个对象被放置到它们与相应章节UILocalizedIndexCollation

ANSWER使用代码:

-(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 2012-03-23 19:31:18

+0

你能把代码放在这里吗? – Shmidt 2012-12-06 11:33:51

+0

@Flink你是什么意思?代码在上面发布。 – Bot 2012-12-06 17:46:33

回答

1

你应该可以用比较方法来做到这一点。

的一个很好的例子在这里提供:https://stackoverflow.com/a/805589/580291

+0

这工作完美。我认为我将不得不使用它与整理排序,但NOPE!谢谢! – Bot 2012-03-23 20:29:00