排序的几种方法 oc
生活随笔
收集整理的這篇文章主要介紹了
排序的几种方法 oc
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
NSArray?*arr?=?@[ @"234", @"123", @"235", @"133" ]; NSArray?*sortArr?=?[arr?sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"%@",sortArr);//用要排序的原數(shù)組調(diào)用實例方法,第二個參數(shù),是方法選擇,如果數(shù)組的元素都是字符串,那么直接用compare:就行 //block(代碼塊)排序 NSArray?*sortArr2?=?[arr?sortedArrayUsingComparator:^NSComparisonResult(id?obj1,?id?obj2)?{ return?[obj1?compare:obj2];//根據(jù)比較結(jié)果,如果結(jié)果是1,則交換 }]; NSLog(@"%@",sortArr2); main函數(shù) Student *s = [[Student alloc] init];
??????? s.name = @"zhangyu";
??????? s.age = 20;
???????
??????? Student *s1 = [[Student alloc] init];
???????
??????? s1.name = @"zhangyuzi";
??????? s1.age = 18;
???????
??????? Student *s2 = [[Student alloc] init];
??????? s2.age = 21;
??????? s2.name = @"lirongsheng";
???????
??????? NSArray *arr = @[
???????????????????????? s,
???????????????????????? s1,
???????????????????????? s2
???????????????????????? ];
???????
???????
??????? //自定義對象排序
??????? NSArray *stuArr = [arr sortedArrayUsingSelector:@selector(compareWithAge:)];
??????? NSLog(@"%@",stuArr);
??? }
??? return 0; } main.h #import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) int age;
//自定義比較方法(排序時調(diào)用),要遵循compare:方法
//格式:-(NSComparisonResult)方法名:(要比較的類名 *)參數(shù)名
-(NSComparisonResult)compareWithName:(Student *)stu;
-(NSComparisonResult)compareWithAge:(Student *)stu;
@end main.m #import "Student.h"
@implementation Student
-(NSString *)description{
??? return [NSString stringWithFormat:@"%@,%d",_name,_age];
}
//比較方法的實現(xiàn)
-(NSComparisonResult)compareWithName:(Student *)stu{
??? //對象的比較結(jié)果就相當(dāng)于屬性比較結(jié)果,self,最開始代表第0個元素,后面再比較時,代表交換后的滿足條件的對象
??? /*
???? 0? self a[0];
???? 1? self a[1]
???? 2? self a[2]
???? */
??? //當(dāng)self放在第一個參數(shù)是,代表升序,放在第二個參數(shù)時,代表降序??? 只有result為1的時候才進行交換
??? NSComparisonResult result = [stu.name compare: self.name];
???
??? return result;
}
-(NSComparisonResult)compareWithAge:(Student *)stu{
??? NSComparisonResult result;
???
??? if (self.age < stu.age) {
??????? result = 1;
??? }else{
??????? result = -1;
??? }
???
??? return? result;
}
@end
??????? s.name = @"zhangyu";
??????? s.age = 20;
???????
??????? Student *s1 = [[Student alloc] init];
???????
??????? s1.name = @"zhangyuzi";
??????? s1.age = 18;
???????
??????? Student *s2 = [[Student alloc] init];
??????? s2.age = 21;
??????? s2.name = @"lirongsheng";
???????
??????? NSArray *arr = @[
???????????????????????? s,
???????????????????????? s1,
???????????????????????? s2
???????????????????????? ];
???????
???????
??????? //自定義對象排序
??????? NSArray *stuArr = [arr sortedArrayUsingSelector:@selector(compareWithAge:)];
??????? NSLog(@"%@",stuArr);
??? }
??? return 0; } main.h #import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) int age;
//自定義比較方法(排序時調(diào)用),要遵循compare:方法
//格式:-(NSComparisonResult)方法名:(要比較的類名 *)參數(shù)名
-(NSComparisonResult)compareWithName:(Student *)stu;
-(NSComparisonResult)compareWithAge:(Student *)stu;
@end main.m #import "Student.h"
@implementation Student
-(NSString *)description{
??? return [NSString stringWithFormat:@"%@,%d",_name,_age];
}
//比較方法的實現(xiàn)
-(NSComparisonResult)compareWithName:(Student *)stu{
??? //對象的比較結(jié)果就相當(dāng)于屬性比較結(jié)果,self,最開始代表第0個元素,后面再比較時,代表交換后的滿足條件的對象
??? /*
???? 0? self a[0];
???? 1? self a[1]
???? 2? self a[2]
???? */
??? //當(dāng)self放在第一個參數(shù)是,代表升序,放在第二個參數(shù)時,代表降序??? 只有result為1的時候才進行交換
??? NSComparisonResult result = [stu.name compare: self.name];
???
??? return result;
}
-(NSComparisonResult)compareWithAge:(Student *)stu{
??? NSComparisonResult result;
???
??? if (self.age < stu.age) {
??????? result = 1;
??? }else{
??????? result = -1;
??? }
???
??? return? result;
}
@end
轉(zhuǎn)載于:https://www.cnblogs.com/zhangyu666666/p/4931124.html
總結(jié)
以上是生活随笔為你收集整理的排序的几种方法 oc的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse设置自定义快捷键
- 下一篇: hdu 2510