0

i'm wondering on how to sort Chinese character into "#" instead of A-Z.

any comments are greatly appreciated.

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
    self.collation = [UILocalizedIndexedCollation currentCollation];
    NSInteger sectionCount = [[self.collation sectionTitles] count];//section count is take from sectionTitles and not sectionIndexTitles
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
    //create an array to hold the data for each section
    for(int i = 0; i < sectionCount; i++)
    {
        [unsortedSections addObject:[NSMutableArray array]];
    }
    //put each object into a section
    for (id object in array)
    {
        NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector];
        [[unsortedSections objectAtIndex:index] addObject:object];
    }
    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
    //sort each section
    for (NSMutableArray *section in unsortedSections)
    {
        [sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]];
    }
    return sections;
}

enter image description here

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Desmond
  • 5,001
  • 14
  • 56
  • 115

1 Answers1

0

This is what i ended up doing

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
    self.collation = [UILocalizedIndexedCollation currentCollation];
    NSInteger sectionCount = [[self.collation sectionTitles] count];//section count is take from sectionTitles and not sectionIndexTitles
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];

    //create an array to hold the data for each section
    for(int i = 0; i < sectionCount; i++)
    {
        [unsortedSections addObject:[NSMutableArray array]];
    }

    if ([self.catString isEqualToString:ARTISTS])
    {
        //put each object into a section
        for (id object in array)
        {
            if (!object)
            {
                continue;
            }
            NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector];
            [[unsortedSections objectAtIndex:index] addObject:object];
        }
    }
    else
    {
        NSInteger index;
        for (id object in array)
        {
            Song *songItem = object;
            NSString* charIndex;

            if([songItem.songName length]<=2)
            {
                charIndex = [songItem.songName substringToIndex:1];
            }
            else if([songItem.songName length]<=3)
            {
                charIndex = [songItem.songName substringToIndex:2];
            }
            else if([songItem.songName length]<=4)
            {
                charIndex = [songItem.songName substringToIndex:3];
            }
            else if([songItem.songName length]>=5)
            {
                charIndex = [songItem.songName substringToIndex:4];
            }
            NSRegularExpression *regex = [[NSRegularExpression alloc]
                                           initWithPattern:@"[a-zA-Z]" options:0 error:NULL];
            NSUInteger matches = [regex numberOfMatchesInString:charIndex options:0
                                                          range:NSMakeRange(0, [charIndex length])];
            if (matches >=2)
            {
                index = [self.collation sectionForObject:object collationStringSelector:selector];
                [[unsortedSections objectAtIndex:index] addObject:object];
            }
            else
            {
                index = 26; //add object to last index "#"
                [[unsortedSections objectAtIndex:index] addObject:object];
            }
            songItem = nil;
        }
    }
    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];

    //sort each section
    for (NSMutableArray *section in unsortedSections)
    {
        [sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]];
    }
    return sections;
}
Desmond
  • 5,001
  • 14
  • 56
  • 115
  • I guess my question would be why you're using UILocalizedIndexedCollation at all. – matt Feb 03 '14 at 04:45
  • for the indexing, but i want the chinese character sorted into "#" instead – Desmond Feb 03 '14 at 04:56
  • "For the indexing" But you are doing all the indexing yourself. Your data are _not_ localized. It's just English alphabet plus the rule about chinese. You are _fighting_ UILocalizedIndexedCollation, not _using_ it. You'd have an easier time if UILocalizedIndexedCollation were not in the story at all. – matt Feb 03 '14 at 05:13
  • Here's how to just build your own section-based table view model data with A-Z sections: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p416sections/ch21p718sections/RootViewController.m You'd have a simpler time if you just adapted the `for` loop shown in the `viewDidLoad` implementation to add chinese title handling. – matt Feb 03 '14 at 05:25
  • @matt the UILocalizedIndexedCollation works fine, but i had a hard time to sort like what iPod did. for non A-Z to "#" i initially did the method of the for loop you had mentioned before... – Desmond Feb 03 '14 at 06:04