日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NSArray利用Cocoa框架进行汉字排序

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NSArray利用Cocoa框架进行汉字排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
NSArray利用Cocoa框架進行漢字排序

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 = [super init];

? ? 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 = [[Student alloc] initWithName:stuName andStuScore:stuScore andStuSex:stuSex andStuAge:stuAge];

? ? return stu;


}


@end

main.m

Student *stu1 = [[Student alloc] initWithName:@"電腦" andStuScore:34.5 andStuSex:@"" andStuAge:20];

? ? Student *stu2 = [[Student alloc] initWithName:@"鼠標" andStuScore:34.7 andStuSex:@"" andStuAge:21];

? ? Student *stu3 = [[Student alloc] initWithName:@"鍵盤" andStuScore:45.6 andStuSex:@"nan" andStuAge:22];

? ? Student *stu4 = [[Student alloc] initWithName:@"顯示器" andStuScore:34.6 andStuSex:@"" andStuAge:23];

? ? NSArray *stuArray1 = [[NSArray alloc]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.stuName localizedCompare:stu2.stuName];

? ? }];

? ? NSLog(@"未排序前:");

? ? for (Student *stu in stuArray1) {

? ? ? ? NSLog(@"name = %@,score = %g,sex = %@,age = %ld",stu.stuName,stu.stuScore,stu.stuSex,stu.stuAge);

? ? }

? ? NSLog(@"排序后");

? ? for (Student *stu in newArry) {

? ? ? ? NSLog(@"name = %@,score = %g,sex = %@,age = %ld",stu.stuName,stu.stuScore,stu.stuSex,stu.stuAge);

? ? }

? ? return 0;



這樣做會有幾方面的優點:1 支持多個漢字按字母序排序(若第一個字的第一個字母同樣。則按第一個字的第二個字母比較,若第一個字的字母全然同樣,按第二個字的首字母繼續排序)。 2原本可能須要保存漢字拼音的地方。如今不須要了。

3 能夠通過對nickNameSortde進一步定制。完畢更復雜的比較,比方先比較會員狀態,在按姓名字母序完畢比較。4總體結構簡單 使用的都是CocaTouch框架下的的方法。



posted on 2017-08-07 20:42 mthoutai 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/mthoutai/p/7301248.html

總結

以上是生活随笔為你收集整理的NSArray利用Cocoa框架进行汉字排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。