得到汉字首字母在表中的顺序位置

根据英文字母排序很简单,但是通过汉字首字母排序就比较费劲,网友用C写了一个算法,能将汉字转为英文然后比较;
项目中用到的是addressbook中的好友名字按姓氏索引显示 
关键词是:UILocalizedIndexedCollation
搜索实现中文下的UITableView Index就能搜到关于这点知识

    Person *per = [[Person alloc] init];
    per.name = @"欧库作";
    
    Person *per2 = [[Person alloc] init];
    per2.name = @"比比";
    
    Person *per3 = [[Person alloc] init];
    per3.name = @"阿门";
    
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:per,per2,per3, nil];
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    for (id obj in array) {
        int index = [collation sectionForObject:obj collationStringSelector:@selector(getname)];
        NSLog(@"index:%i",index);
    }
index就是所在的位置,测试时需要引入pinyin.h文件和pinyin.c文件,写一个Person的类,有一个name的属性;

#import <Foundation/Foundation.h>
#import "Person.h"

@interface Person : NSObject

@property(nonatomic,retain)NSString* name;

@end
#import "Person.h"
#import "pinyin.h"

@implementation Person
@synthesize name;

- (NSString *)getname{//不是重写get方法
    if ([name canBeConvertedToEncoding:NSASCIIStringEncoding]) {
        return name;
    }else {
        return [NSString stringWithFormat:@"%c",pinyinFirstLetter([name characterAtIndex:0])];
    }
}
原文地址:https://www.cnblogs.com/neworiginou/p/2852964.html