OC中常用排序方法

2021-07-25 16:57:43 字數 4457 閱讀 6562

**:

①.oc中常用排序方法:

1).不可變陣列

- (nsarray *)sortedarrayusingselector:(sel)comparator;

- (nsarray *)sortedarrayusingcomparator:(nscomparator)cmptr;

2)可變陣列

- (void)sortusingselector:(sel)comparator;

- (void)sortusingcomparator:(nscomparator)cmptr;

3).字典排序

- (nsarray *)keyssortedbyvalueusingcomparator:(nscomparator)cmptr 

- (nsarray *)keyssortedbyvalueusingselector:(sel)comparator;

②應用

1).不可變陣列排序:(方法1)

nsarray *arr = @[@"aa",@"rr",@"pp",@"hh",@"xx",@"vv"];

//用系統的方法進行排序,系統缺少兩個元素比較的方法.

//selector方法選擇器.

nsarray *sortarr = [arr sortedarrayusingselector:@selector(compare:)];

nslog(@"%@",sortarr);

方法2:block塊語法

[arr sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2) ];

nslog(@"%@",arr);

}

2).可變陣列排序:方法1

nsmutablearray *arr = [@[@54 ,@33,@12,@23,@65] mutablecopy];

[arr sortusingselector:@selector(compare:)];//compare陣列中兩個元素比較的方法

nslog(@"%@",arr);

方法2
[arr sortusingcomparator:^nscomparisonresult(id obj1, id obj2) ];

nslog(@"%@",arr);

}

注:字典方法類似

③.例題:定義乙個學生物件,對學生物件按照,姓名,年齡,成績,學號進行排序,(兩種排序方式)

方法1:(用selector方法選擇器,需要重新定義comparator)

**如下:

1.物件宣告(student.h中)

@property (nonatomic, retain) nsstring *name;

@property (nonatomic, assign) nsinteger age;

@property (nonatomic, assign) cgfloat score;

@property (nonatomic, assign) nsinteger number;

//初始化

- (id)initwithname:(nsstring *)name age:(nsinteger)age score:

(cgfloat)score number:(nsinteger)number;

//便利構造器

+ (id)studentwithname:(nsstring *)name age:(nsinteger)age score:

(cgfloat)score number:(nsinteger)number;

//兩個學生按照年齡比較的方法

- (nscomparisonresult)comparebyage:(student *)stu;

//兩個學生按照姓名比較的方法

- (nscomparisonresult)comparebyname:(student *)stu;

//兩個學生按照成績比較的方法

- (nscomparisonresult)comparebyscore:(student *)stu;

//兩個學生按照學號比較的方法

- (nscomparisonresult)comparebynumber:(student *)stu;

2.實現(student.m檔案)

- (id)initwithname:(nsstring *)name age:(nsinteger)age score:

(cgfloat)score number:(nsinteger)number

return self;

}//便利構造器

+ (id)studentwithname:(nsstring *)name age:(nsinteger)age score:

(cgfloat)score number:(nsinteger)number

//重寫description

- (nsstring *)description

//兩個學生按照年齡比較的方法

- (nscomparisonresult)comparebyage:(student *)stu

//兩個學生按照姓名降序的方法

- (nscomparisonresult)comparebyname:(student *)stu

//兩個學生按照成績降序比較的方法

- (nscomparisonresult)comparebyscore:(student *)stu

//兩個學生按照學號公升序的方法

- (nscomparisonresult)comparebynumber:(student *)stu

主函式(main.h)

student *student1 = [student studentwithname:@"a" age:23 score:21 number:3434343];

student *student2 = [student studentwithname:@"b" age:45 score:432.4 number:324];

student *student3 = [student studentwithname:@"c" age:32 score:4321.4 number:2343];

student *student4 = [student studentwithname:@"d" age:7 score:43.4 number:233];

student *student5 = [student studentwithname:@"e" age:73 score:65 number:2332424];

nsmutablearray *arr = [nsmutablearray arraywithobjects:

student1,student2,student3,student4,student5,nil];

//按照年齡公升序排序

[arr sortusingselector:@selector(comparebyage:)];

nslog(@"%@",arr);

//按照成績降序排序

[arr sortusingselector:@selector(comparebyscore:)];

nslog(@"%@",arr);

//按照姓名降序排序

[arr sortusingselector:@selector(comparebyname:)];

nslog(@"%@",arr);

//按照學號公升序排序

[arr sortusingselector:@selector(comparebynumber:)];

nslog(@"%@",arr);

方法2

.用block塊語法進行排序

//按年齡公升序排序

[arr sortusingcomparator:^(id obj1,id obj2) ];

nslog(@"%@",arr);

//按成績降序排序

[arr sortusingcomparator:^(id obj1,id obj2) ];

nslog(@"%@",arr);

//按姓名降序排序

[arr sortusingcomparator:^(id obj1,id obj2) ];

nslog(@"%@",arr);

//按學號公升序排序

nscomparator sortblock = ^(id obj1,id obj2) ;

[arr sortusingcomparator:sortblock];

nslog(@"%@",arr);

OC的當中常用的類

值物件 作用 把基本資料型別轉換成物件型別 指令 nsinteger a 10 nsnumber number nsnumber alloc initwithinteger a nsnumber nsinteger 指令 nsinteger b number integervalue 或者 nsnu...

ServletContext中常用方法

獲取tomcat的context的初始化引數。1.獲取tomcat的server.xml中設定context的初始化引數。例如 privileged true antiresourcelocking false antijarlocking false debug 0 reloadable true...

jquery中常用方法

tofixed 方法可把 number 四捨五入為指定小數字數的數字。numberobject.tofixed num 引數 描述num 必需。規定小數的位數,是 0 20 之間的值,包括 0 和 20,有些實現可以支援更大的數值範圍。如果省略了該引數,將用 0 代替。使用 type能夠返回更準確的...