【问题标题】:Collation sort with object带有对象的排序规则
【发布时间】:2012-03-23 19:23:56
【问题描述】:

我有一个列表视图,并且想要对数组进行排序,以便每个名称都位于正确的 UILocalizedIndexCollation 部分下。 这是正在传入的部分的 sn-p。我想通过 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

使用的答案代码:

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

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/805547/…
  • 可以把代码放在这里吗?
  • @Flink 你是什么意思?代码贴在上面。
  • @Flink 链接答案中有代码。还是您想要我最终使用的代码?
  • @Flink 我认为编辑是您正在寻找的。不过很久以前,我不确定。

标签: iphone objective-c ios5 uilocalizedcollation


【解决方案1】:

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

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

【讨论】:

  • 这很完美。我以为我必须将它与排序规则一起使用,但不!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 2020-01-27
  • 1970-01-01
  • 2013-01-09
  • 2014-12-13
  • 1970-01-01
相关资源
最近更新 更多