OC NSArray数组排序
一、一般排序
// 排序
NSArray *arr = @[@"",@"",@"",@""];
NSArray *newarr = [arr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",newarr);
二、高级排序(数组中存的是对象,按对象的属性进行排序)
#import <Foundation/Foundation.h> @interface Student : NSObject @property (assign ,nonatomic) int age; //提供一种比较方式
- (NSComparisonResult)compareStudentWithAge:(Student *)tempStudent; @end #import "Student.h" @implementation Student - (NSComparisonResult)compareStudentWithAge:(Student *)tempStudent
{
//方法1:
// NSNumber *num1 = [[NSNumber alloc] initWithInteger:self.age];
// NSNumber *num2 = [[NSNumber alloc] initWithInteger:tempStudent.age];
// return [num1 compare:num2];
//方法2:
if (self.age>tempStudent.age) {
return NSOrderedDescending;
}
else if (self.age<tempStudent.age)
return NSOrderedAscending;
else
return NSOrderedSame;
} -(NSString *)description
{
return [NSString stringWithFormat:@"age:%d",self.age];
} @end #import <Foundation/Foundation.h> #import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool { Student *stu1 = [[Student alloc] init];
stu1.age= ;
Student *stu2 = [[Student alloc] init];
stu2.age= ;
Student *stu3 = [[Student alloc] init];
stu3.age= ; NSArray *stuArr = @[stu1,stu2,stu3];
NSArray *newStuArr = [stuArr sortedArrayUsingSelector:@selector(compareStudentWithAge:)];
NSLog(@"%@",newStuArr);
}
return ;
}
三、超级排序(数组中对象多属性)
#import <Foundation/Foundation.h> @interface People : NSObject @property (assign,nonatomic) NSInteger age;
@property (assign, nonatomic) NSInteger score;
@property (copy,nonatomic) NSString *name; @end #import "People.h" @implementation People - (NSString *)description
{
return [NSString stringWithFormat:@"age:%ld score:%ld name:%@", self.age,self.score,self.name];
} #import <Foundation/Foundation.h>
#import "People.h" int main(int argc, const char * argv[]) {
@autoreleasepool { People *stu1 = [[People alloc] init];
stu1.age = ;
stu1.score = ;
stu1.name = @"bowen1"; People *stu2 = [[People alloc] init];
stu2.age = ;
stu2.score = ;
stu2.name = @"bowen2"; People *stu3 = [[People alloc] init];
stu3.age = ;
stu3.score = ;
stu3.name = @"bowen3"; People *stu4 = [[People alloc] init];
stu4.age = ;
stu4.score = ;
stu4.name = @"bowen4"; People *stu5 = [[People alloc] init];
stu5.age = ;
stu5.score = ;
stu5.name = @"bowen5"; NSArray *arr = @[stu1,stu2,stu3,stu4,stu5]; // 排序描述,数组按属性排序 NSSortDescriptor
NSSortDescriptor *ageSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
NSSortDescriptor *scoreSort = [[NSSortDescriptor alloc] initWithKey:@"score" ascending:YES];
NSSortDescriptor *nameScort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; //使用[排序描述]对数组进行排序
NSArray *newArr = [arr sortedArrayUsingDescriptors:@[ageSort,scoreSort,nameScort]]; NSLog(@"%@",newArr); }
return ;
} @end
OC NSArray数组排序的更多相关文章
- OC NSArray 数组
# OC NSArray 数组 NSArray常用方法 获取数组中第一位元素 array.firstObject 获取数组中最后一个元素 array.lastObject 获取数组中指定索引下标的元素 ...
- OC中数组排序的3种方法
总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 大体上 ...
- iOS - OC NSArray 数组
前言 @interface NSArray<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSe ...
- OC中数组排序总结
过完节回来,感觉很多东西都生疏了.总结一下数组的排序.应该不会太完美,后续添加补充. OC中的数组排序方法其实不太多,要根据不同的使用场景来使用不同的方法.Foundation框架中一般用到一下几个方 ...
- OC NSArray使用
#import <Foundation/Foundation.h> #import "Student.h" #pragma mark 创建一个数组 void array ...
- OC:数组排序、时间格式化字符串
数组排序 //不可变数组的排序 NSArray * arr = [NSArray arrayWithObjects:@"hellow", @"lanou", @ ...
- NSArray 数组排序
//方法1,使用自带的比较器 //compare是数组自带的比较方法 NSArray *array=[NSArray arrayWithObjects:@"3",@"1& ...
- OC——NSArray和NSMutableArray
/*---------------------NSArray---------------------------*/ //创建数组 NSArray *array1 = [NSArray arrayW ...
- 死去活来的OC NSArray 中文排序 及输出
目的 1.NSArray 能够支持中文排序 2.NSLog 能够直接输出 NSArray 内的中文(事实上 java 直接打印数组也不能显示内容哈) 又是死去活来的搞了1个小时,分类实现.废话少说,上 ...
随机推荐
- Idea中Maven仓库配置会自动恢复
手头有好几个项目,关闭一个项目,打开另一个项目,发现又在重新下载jar包,打开设置一看,maven配置又恢复到了.m2下边.idea配置的maven会自动恢复吗? 答案是否定的,idea的设置有两个, ...
- Matchvs 使用记录
Matchvs Matchvs视频教程. https://doc.matchvs.com/VideoTutorials/videogs matchvs下载资源. http://www.matchvs. ...
- hdu 3698 UVA1490 Let the light guide us 线段树优化DP
题目链接 and 题目大意 hdu3698 但是 hdu的数据比较弱,所以在这luogu提交吧UVA1490 Let the light guide us 有一个\(n*m\)的平原,要求每行选一个点 ...
- Mato的文件管理 (莫队)题解
思路: 莫队模板题,转换几次就是找逆序数,用树状数组来储存数就行了 注意要离散化 代码: #include<queue> #include<cstring> #include& ...
- JVM堆内存调优
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...
- 使用PDFminer3k解析pdf为文字遇到:WARING:root:GBK-EUC-H
最近需要把PDF解析为文字,查了查python的模块,发现PDFminer3k能满足需求.我使用的是 windows平台下的python3.6,python2的则下载pdfminer. 首先下载:直接 ...
- ajax用beforeSend自定义请求过程中客户端事件,提高用户体验
本文为博主原创,未经允许不得转载: 在应用ajax的过程中,当我们再前台提交请求的时候,如果服务端响应事件比较长,就会导致需要等很长时间在前台才能接受到服务端返回的 响应结果,往往会导致用户重复点击按 ...
- NRF24L01 射频收发 使用方法
在干啥 这两天在调nrf24l01,最终还是参考正点原子的例程才调通,看芯片手册太难了 还要说啥废话 废话说到这,接下来上代码 SPI协议 spi.c #include "spi.h&quo ...
- poj 2762 Going from u to v or from v to u? trajan+拓扑
Going from u to v or from v to u? Description In order to make their sons brave, Jiajia and Wind t ...
- 升级php7一些需要注意的地方
1.升级过程涉及代码的主要处理的就是几个扩展(mysql.mssql .mcrypt.ereg)使用到的一些废弃函数(call_user_method.call_user_method_array等) ...