一、一般排序

         // 排序
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数组排序的更多相关文章

  1. OC NSArray 数组

    # OC NSArray 数组 NSArray常用方法 获取数组中第一位元素 array.firstObject 获取数组中最后一个元素 array.lastObject 获取数组中指定索引下标的元素 ...

  2. OC中数组排序的3种方法

    总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 大体上 ...

  3. iOS - OC NSArray 数组

    前言 @interface NSArray<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSe ...

  4. OC中数组排序总结

    过完节回来,感觉很多东西都生疏了.总结一下数组的排序.应该不会太完美,后续添加补充. OC中的数组排序方法其实不太多,要根据不同的使用场景来使用不同的方法.Foundation框架中一般用到一下几个方 ...

  5. OC NSArray使用

    #import <Foundation/Foundation.h> #import "Student.h" #pragma mark 创建一个数组 void array ...

  6. OC:数组排序、时间格式化字符串

    数组排序 //不可变数组的排序 NSArray * arr = [NSArray arrayWithObjects:@"hellow", @"lanou", @ ...

  7. NSArray 数组排序

    //方法1,使用自带的比较器 //compare是数组自带的比较方法 NSArray *array=[NSArray arrayWithObjects:@"3",@"1& ...

  8. OC——NSArray和NSMutableArray

    /*---------------------NSArray---------------------------*/ //创建数组 NSArray *array1 = [NSArray arrayW ...

  9. 死去活来的OC NSArray 中文排序 及输出

    目的 1.NSArray 能够支持中文排序 2.NSLog 能够直接输出 NSArray 内的中文(事实上 java 直接打印数组也不能显示内容哈) 又是死去活来的搞了1个小时,分类实现.废话少说,上 ...

随机推荐

  1. c++ sleep(windows/linux)

    c标准中包含了一个sleep用以实现当前线程暂停执行n毫秒,如下所示: 函数名: sleep 功 能: 执行挂起一段时间 用 法: unsigned sleep(unsigned seconds); ...

  2. RabbitMQ详解(一)------简介与安装(Docker)

    RABBITMQ详解(一)------简介与安装(DOCKER) 刚刚进入实习,在学习过程中没有接触过MQ,RabbitMQ 这个消息中间件,正好公司最近的项目中有用到,学习了解一下. 首先什么是MQ ...

  3. 基于qml创建最简单的图像处理程序(3)-使用opencv&qml进行图像处理

    <基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/83 ...

  4. 20145305 《网络对抗》MSF基础应用

    实践过程及结果截图 主动攻击ms08_067 Kali的IP:10.43.46.93 靶机的IP:10.43.49.28 MS08_067远程漏洞攻击实践:Shell 攻击成功的结果 在kali上执行 ...

  5. 20145331魏澍琛《网络对抗》Exp4 恶意代码分析

    20145331魏澍琛<网络对抗>Exp4 恶意代码分析 基础问题回答 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作 ...

  6. Java中线程出现Exception in thread "Thread-0" java.lang.IllegalMonitorStateException异常 解决方法

    代码 package thread; public class TestChongNeng { public static void main(String[] args) { Thread t1 = ...

  7. Java 多线程查找文件中的内容

    学过了操作系统,突然不知道多线程有什么用了. 看了一下百度,发现多线程,可以提升系统利用率 在系统进行IO操作的时候,CPU可以处理一些其他的东西,等IO读取到内存后,CPU再处理之前的操作. 总之可 ...

  8. spring boot application.properties/application.yml 配置属性大全

    来自官网  https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.h ...

  9. BZOJ 1003: [ZJOI2006]物流运输(spfa+dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1003 题意: 思路: 首先用spfa计算一下任意两天之内的最短路,dis[a][b]表示的就是在第a ...

  10. UVa 1151 买还是建

    https://vjudge.net/problem/UVA-1151 题意: 平面上有n个点,你的任务是让所有n个点连通.为此,你可以新建一些边,费用等于两个端点的距离平方和.另外还有q个套餐可以购 ...