Iphone 带对象的排序规则排序

Iphone 带对象的排序规则排序,iphone,objective-c,ios5,uilocalizedcollation,Iphone,Objective C,Ios5,Uilocalizedcollation,我有一个列表视图,希望对数组进行排序,以便每个名称都位于正确的UILocalizedIndexCollation部分。 下面是正在传入的部分的一个片段。我希望通过名称进行排序,但我也希望保持对象的完整性,以便使用其他数据 2012-03-23 12:10:14.083 MyApp[61241:14803] Section: ( { "c_id" = 261; "customer_id" = 178664; "first_name"

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

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

使用的应答代码:

-(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;
}
-(NSArray*)分区对象:(NSArray*)数组排序规则字符串选择器:(SEL)选择器
{
UILocalizedIndexedCollation*排序=[UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount=[[collation sectionTitles]count];
NSMUTABLEARRY*unsortedSections=[NSMUTABLEARRY阵列容量:sectionCount];
对于(int i=0;i
您应该能够使用比较方法进行比较


这里提供了一个很好的例子:

这可能会有帮助:您能把代码放在这里吗?@Flink您是什么意思?代码贴在上面。@Flink链接的答案中有代码。还是你想要我最终使用的代码?@Flink我想编辑就是你想要的。但我不确定,这是很久以前的事了。这很好用。我原以为我必须将它与排序规则一起使用,但没有!谢谢
-(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;
}