NSArray数字和排序
1.数字
int main(int argc, const char * argv[]) { @autoreleasepool { //添加空白 [NSNull null] // NSArray *arr=[NSArray arrayWithObjects:@1,@2,[NSNull null],@3,nil]; // for (id a in arr) { // NSLog(@"%@",a); // // } //添加数字 // NSNumber *arr=[NSNumber numberWithInt:10]; // NSLog(@"%@",arr); //数值类型转整形 // NSLog(@"%d",[arr intValue]); NSNumber *num1=[NSNumber numberWithInt:10]; NSNumber *num2=[NSNumber numberWithInt:20]; NSNumber *num3=[NSNumber numberWithInt:30]; NSArray *numArr=[NSArray arrayWithObjects:num1,num2,num3,nil]; for (id a in numArr) { // NSLog(@"%@",a); NSLog(@"%d",[a intValue]+100); } // NSArray *arr=[NSArray arrayWithObjects:@1,@2,@3,nil]; } return 0; }
2.排序1
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // NSArray *arr=@[@"lisi",@"wangwu",@"zhansan"]; // NSLog(@"%@",arr); // NSArray *str=@[@"ABC",@"BCD",@"FDFSFDS",@"ACV"]; // NSArray *nums=@[@12,@10,@60,@20,@30]; //不可变集合的数组排序 // NSArray *sortedArray = [nums sortedArrayUsingSelector:@selector(compare::)]; // NSLog(@"%@",sortedArray); } return 0; }
2.1类排序
#import <Foundation/Foundation.h> #import "Student.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *sunwukong=[[Student alloc] init]; sunwukong.name=@"sunwukong"; sunwukong.age= 1000; Student *zhubajie=[[Student alloc] init]; zhubajie.name=@"zhubajie"; zhubajie.age=1800; Student *shaheshang=[[Student alloc] init]; shaheshang.name=@"shaheshang"; shaheshang.age=1200; NSArray *qujignTeam=@[sunwukong,zhubajie,shaheshang]; //选择器排序法 /** * 通过自定义的选择器实现按照姓名的升序排序 * * @param mySortNameAsc: 自定义排序方法 * * @return */ // NSArray *newTeam=[qujignTeam sortedArrayUsingSelector:@selector(mySortNameAsc:)]; // for (Student *team in newTeam) { // NSLog(@"%@",team); // } //年龄排序 // NSArray *newAge=[qujignTeam sortedArrayUsingSelector:@selector(mySortAgeAsc:)]; // for (Student *Aage in newAge) { // NSLog(@"%@",Aage); // } /** * 描述信息排序 */ NSSortDescriptor *sortDesc=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; NSArray *newTeam=[qujignTeam sortedArrayUsingDescriptors:@[sortDesc]]; for (Student *s in newTeam) { NSLog(@"%@",s); } }
NSArray *newTeam1=[qujignTeam sortedArrayUsingComparator:^NSComparisonResult(Student *obj1,Student *obj2) {
return [obj1.name compare:obj2.name];
}];
for (Student *stu in newTeam1) {
NSLog(@"%@",stu);
}
return 0;
}
#import <Foundation/Foundation.h> @interface Student : NSObject @property(strong,nonatomic)NSString *name; @property(assign,nonatomic)int age; /** * 按照姓名的升序排序 * * @param obj stu对象 * * @return 枚举 -1,0,+1 */ -(NSComparisonResult)mySortNameAsc:(Student *) obj; -(NSComparisonResult)mySortNameDesc:(Student *) obj; -(NSComparisonResult)mySortAgeAsc:(Student *) obj; -(NSComparisonResult)mySortAgeDesc:(Student *)obj; @end
#import "Student.h" @implementation Student - (NSString *)description { return [NSString stringWithFormat:@"%@,%d", _name,_age]; } -(NSComparisonResult)mySortNameAsc:(Student *) obj{ //通过姓名属性进行比较 return [self.name compare:obj.name]; } -(NSComparisonResult)mySortNameDesc:(Student *)obj{ return [obj.name compare:self.name]; } /** * 按照年龄进行升序排序 * * @param obj stu对象 * * @return 返回升序的结果 */ -(NSComparisonResult)mySortAgeAsc:(Student *)obj{ if(self.age>obj.age){ return NSOrderedDescending; }else if(self.age<obj.age){ return NSOrderedAscending; }else{ return NSOrderedSame; } } -(NSComparisonResult)mySortAgeDesc:(Student *)obj{ if(self.age>obj.age){ return NSOrderedAscending; }else if(self.age<obj.age){ return NSOrderedDescending; }else{ return NSOrderedSame; } } @end