在学习tableview的使用中,涉及到了通讯录中中文姓名的排序问题,现在网上好像大部分方法,都是使用一位大牛写的pinyin.h文件,将每个汉字对应的拼音首字母映射了起来。这个方法在仅仅进行首字母的排序上没有问题,但是如果首字母相同就不好判断了,比如兰和刘,王和吴。后来发现使用CFStringTransform可以方便的进行全部拼音的比较。代码如下:
首先增加一个StringCompareUtil的类
StringCompareUtil.h
#import <Foundation/Foundation.h>
@interface NSArray (chinese)
+(NSArray*) sortedArrayByChinese: (NSArray*) array;
@end
@interface StringCompareUtil : NSObject
+ (NSComparisonResult) CompareString: (NSString*) strSrc with:(NSString*) str2;
@end
StringCompareUtil.cpp
#import "StringCompareUtil.h"
@interface StringCompareUtil ()
@end
@implementation StringCompareUtil
+ (NSComparisonResult) CompareString: (NSString*) strSrc with:(NSString*) str2
{
CFStringRef cfstringSrc = CFStringCreateWithCString(NULL, strSrc.UTF8String, kCFStringEncodingUTF8);
CFMutableStringRef src = CFStringCreateMutableCopy(NULL, 0, cfstringSrc);
CFStringTransform(src, 0, kCFStringTransformToLatin, false);
CFStringTransform(src, 0, kCFStringTransformStripCombiningMarks, false);
CFStringRef cfstringto = CFStringCreateWithCString(NULL, str2.UTF8String, kCFStringEncodingUTF8);
CFMutableStringRef srcto = CFStringCreateMutableCopy(NULL, 0, cfstringto);
CFStringTransform(srcto, 0, kCFStringTransformToLatin, false);
CFStringTransform(srcto, 0, kCFStringTransformStripCombiningMarks, false);
CFComparisonResult result = CFStringCompare(src, srcto, kCFCompareCaseInsensitive);
NSComparisonResult r = (NSComparisonResult)result;
return r;
}
@end
@implementation NSArray (chinese)
+(NSArray*) sortedArrayByChinese: (NSArray*) array
{
NSArray* ret = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSComparisonResult ret = [StringCompareUtil CompareString:obj1 with:obj2];
return ret;
}];
return ret;
}
@end
<dict>
<key>backup</key>
<string>平西王</string>
<key>name</key>
<string>吴三桂</string>
<key>number</key>
<string>137899889754</string>
</dict>
<dict>
<key>backup</key>
<string>老农名</string>
<key>name</key>
<string>王大伯</string>
<key>number</key>
<string>13789897654</string>
</dict>
//然后在一个保存姓名信息的model类,读取姓名信息,并对姓名进行排序。
#import "ContactInfo.h"
#import "StringCompareUtil.h"
@interface ContactInfo ()
@property( strong,nonatomic) NSMutableArray* sortedData;
@property( strong, nonatomic) NSString* localDataFile;
@end
@implementation ContactInfo
-(id) init
{
if (self = [super init]) {
NSArray* a = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE);
NSString* documentPath = [a objectAtIndex:0];
self.localDataFile = [documentPath stringByAppendingPathComponent:@"contactlist.plist"];
self.sortedData = [NSMutableArray arrayWithContentsOfFile:self.localDataFile];
if (self.sortedData.count == 0) {
NSString* contactPlist = [[NSBundle mainBundle] pathForResource:@"ContactList" ofType:@"plist"];
//从文件中读取姓名信息到array
NSMutableArray* contactData = [NSMutableArray arrayWithContentsOfFile:contactPlist];
//对array中的信息,按照姓名拼音进行排序
NSArray* sorted = [contactData sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString* name1 = [(NSDictionary*)obj1 objectForKey:@"name"];
NSString* name2 = [(NSDictionary*)obj2 objectForKey:@"name"];
NSComparisonResult ret = [StringCompareUtil CompareString:name1 with:name2];
return ret;
}];
self.sortedData = [NSMutableArray arrayWithArray:sorted];
}
}
return self;
}
- (NSArray*) sortedPeopleInfo
{
return self.sortedData;
}
以上就是具体的使用方法。我在仿真机上是可以正常编译运行的。由于本人是初学IOS,不知道是不是这个方法,是在IOS中会有什么限制,知道的兄弟麻烦留言告诉我一下~。