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.m

Student *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框架下的的方法。



1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 4年车检过期了怎么办 车检过期了1周怎么办 超过检车几天了怎么办 驾驶证扣两个6分怎么办 行驶证检验有效期过期怎么办 驾驶证被扣26分怎么办 c1驾驶证扣26分怎么办 驾驶证被扣6分后怎么办 c1驾照年审过期一天怎么办 审驾照时间过了怎么办 b2驾驶证扣了分怎么办 a2驾照逾期未审怎么办 中学生只想打游戏不肯学习怎么办 汽车4年未年检怎么办 2年没有验车怎么办 驾驶证过期2年半怎么办 审车逾期一个月怎么办 摩托车驾驶证年审过期一个月怎么办 摩托车驾驶证记满12分怎么办 b2驾驶证扣2分该怎么办 b2扣6分以上怎么办 审驾照晚了3天怎么办 考驾驶证3年到期怎么办 学习驾驶证明过期了怎么办 a2扣了12分怎么办 驾照a2扣6分了怎么办 a2本扣9分怎么办 驾驶证分扣3分怎么办? 异地换驾驶证没有居住证怎么办 b2开c1车扣分怎么办 驾照五次没考过怎么办 大车行驶证丢了怎么办 车的产权证丢了怎么办 车子行驶证掉了怎么办 定期的存折丢了怎么办 存折密码输错6次怎么办 营业执照原件丢失怎么办怎么注销 违章扣了14分怎么办 c1驾驶本过期了怎么办 考驾照没带身份证怎么办 上海扣满12分怎么办