0

I have a list view and want to sort the array so that each of the names is under the correct UILocalizedIndexCollation section. Here is a snippet of the section that is being passed in. I want to sort via the name however I also want to keep the object intact so I can user other data.

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

Partition

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

Calling the partition

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

I am needing to keep the object intact but sort by name and have each of the objects be placed into their appropriate sections with 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;
}
6
  • This might be helpful: stackoverflow.com/questions/805547/…
    – benuuu
    Mar 23, 2012 at 19:31
  • Can you please place the code here?
    – Shmidt
    Dec 6, 2012 at 11:33
  • @Flink what do you mean? The code is posted above.
    – Bot
    Dec 6, 2012 at 17:46
  • @Flink there is code in the linked answer. Or are you wanting the code that I ended up using?
    – Bot
    Dec 6, 2012 at 18:29
  • 1
    @Flink I think the edit is what you are looking for. Been so long ago though, I am not sure.
    – Bot
    Dec 7, 2012 at 17:07

1 Answer 1

1

You should be able to do it with a compare method.

A good example of that is provided here: https://stackoverflow.com/a/805589/580291

1
  • This worked perfect. I thought I would have to use it with a collation sort but NOPE! Thanks!
    – Bot
    Mar 23, 2012 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.