NSArray利用Cocoa框架进行汉字排序
来源:互联网 发布:淘宝好的女装店 编辑:程序博客网 时间:2024/05/17 07:47
NSArray利用Cocoa框架进行汉字排序
在NSString有一个函数localizedCompare:,它的功能是通过自身与给定字符串的比较,返回一个本地化的比较结果,也就是说这个函数是支持汉字比较的。
Student.h
@interface Student :NSObject
@property(nonatomic,copy)NSString *stuName;
@property(nonatomic,assign)CGFloat stuScore;
@property(nonatomic,copy)NSString *stuSex;
@property(nonatomic,assign)NSInteger stuAge;
-(id)initWithName:(NSString *)stuName
andStuScore:(CGFloat) stuScore
andStuSex:(NSString *) stuSex
andStuAge:(NSInteger) stuAge;
+(id)StudentWithName:(NSString *)stuName
andStuScore:(CGFloat) stuScore
andStuSex:(NSString *) stuSex
andStuAge:(NSInteger) stuAge;
@end
Student.m@implementation Student
-(id)initWithName:(NSString *)stuName
andStuScore:(CGFloat) stuScore
andStuSex:(NSString *) stuSex
andStuAge:(NSInteger) stuAge{
self = [superinit];
if (self) {
_stuName = stuName;
_stuScore = stuScore;
_stuSex = stuSex;
_stuAge = stuAge;
}
return self;
}
+(id)StudentWithName:(NSString *)stuName
andStuScore:(CGFloat) stuScore
andStuSex:(NSString *) stuSex
andStuAge:(NSInteger) stuAge{
Student *stu = [[Studentalloc] initWithName:stuNameandStuScore:stuScore andStuSex:stuSex andStuAge:stuAge];
return stu;
}
@end
main.mStudent *stu1 = [[Studentalloc]initWithName:@"电脑"andStuScore:34.5andStuSex:@"男"andStuAge:20];
Student *stu2 = [[Studentalloc] initWithName:@"鼠标"andStuScore:34.7andStuSex:@"男"andStuAge:21];
Student *stu3 = [[Studentalloc] initWithName:@"键盘"andStuScore:45.6andStuSex:@"nan"andStuAge:22];
Student *stu4 = [[Studentalloc] initWithName:@"显示器"andStuScore:34.6andStuSex:@"男"andStuAge:23];
NSArray *stuArray1 = [[NSArrayalloc]initWithObjects:stu1,stu2,stu3,stu4,nil];
NSArray *newArry = [stuArray1 sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
Student *stu1,*stu2;
stu1 = (Student *)obj1;
stu2 = (Student *)obj2;
return [stu1.stuNamelocalizedCompare:stu2.stuName];
}];
NSLog(@"未排序前:");
for (Student *stuin stuArray1) {
NSLog(@"name = %@,score = %g,sex = %@,age = %ld",stu.stuName,stu.stuScore,stu.stuSex,stu.stuAge);
}
NSLog(@"排序后");
for (Student *stuin newArry) {
NSLog(@"name = %@,score = %g,sex = %@,age = %ld",stu.stuName,stu.stuScore,stu.stuSex,stu.stuAge);
}
return 0;
这样做会有几方面的好处:1 支持多个汉字按字母序排序(若第一个字的第一个字母相同,则按第一个字的第二个字母比较,若第一个字的字母完全相同,按第二个字的首字母继续排序)。 2原本可能需要保存汉字拼音的地方,现在不需要了。 3可以通过对nickNameSortde进一步定制,完成更复杂的比较,比如先比较会员状态,在按姓名字母序完成比较。4整体结构简单使用的都是CocaTouch框架下的的方法。
- NSArray利用Cocoa框架进行汉字排序
- NSArray进行汉字排序
- Cocoa Foundation 框架:NSArray数组的使用
- NSArray中的数据进行排序
- 完成将vc利用word对象的排序功能对输入的汉字进行排序
- NSArray中的数据进行排序2
- NSArray中的数据进行排序 ---- sortedArrayUsingDescriptors
- [ios]NSArray中的数据进行排序 ---- sortedArrayUsingDescriptors
- 对汉字进行排序(RuleBasedCollator)
- 利用java对基本汉字(unicode编码4E00-9FA5)进行笔画排序
- 利用Comparable进行排序
- 利用TreeMap进行排序
- 利用sort进行排序
- 利用MapReduce进行排序
- NSArray 排序
- NSArray排序
- NSArray排序
- NSArray排序
- spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差
- Android组件在代码中修改高度
- 判断空间上三个点是否共线问题【找bug篇】
- debian linux 系统安装镜像 ISO 文件下载集合
- 基于MINA框架快速开发网络应用程序
- NSArray利用Cocoa框架进行汉字排序
- 怎么优化ListView?
- 图片随机小角度堆叠效果,可用于相册缩略图叠加样式【长宽最优化】
- [算法系列之三十二]1的数目
- My First Day
- 4种方法让SpringMVC接收多个对象(转:http://blog.csdn.net/lutinghuan/article/details/46820023)
- 银行系统
- Ant高级-path和fileset
- SQL%NOTFOUND