一、一般排序

         // 排序
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. bzoj1658: [Usaco2006 Mar]Water Slides 滑水

    Description It's a hot summer day, and Farmer John is letting Betsy go to the water park where she i ...

  2. 散列表(HashTable)

    散列表 i. 散列函数 i. 冲突解决 ii. 分离链表法 ii. 开放地址法 iii. 线性探测法 iii. 平方探测法 iii. 双散列 ii. 再散列 ii. 可扩散列 i. 装填因子:元素个数 ...

  3. 20145302张薇 《网络对抗技术》 web安全基础实践

    20145302张薇 <网络对抗技术> web安全基础实践 实验问题回答 1.SQL注入攻击原理,如何防御 原理:攻击者把SQL命令插入到网页的各种查询字符串处,达到欺骗服务器执行恶意的S ...

  4. Wireshark分析RabbitMQ

    消费者Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  5. LightOJ 1393 Crazy Calendar(博弈)题解

    题意:r*c方格中,每个格子有一定石子,每次移动每格任意数量石子,只能向下或者向右动一格,不能移动为败 思路:显然是Nim,到右下曼哈顿距离为偶数的不用管,因为先手动一下后手动一下最后移到右下后还是先 ...

  6. TeeChart入门

    此链接可以作为参考http://tech.sina.com.cn/s/2008-07-07/1612722495.shtml 需要添加引用 using Steema.TeeChart;//tchart ...

  7. [Shiro] - 基于URL配置动态权限

    基于shiro进阶 更改了数据库表 之前的PageController是通过@RequiresPermissions和@RequiresRoles进行是否有权限/是否有角色的判定调用@RequestM ...

  8. IIS中添加ftp站点

    1.创建Windows账号 右击点击“我的电脑”,选择“管理”打开服务器管理的控制台.展开“服务器管理器”,一路展开“配置”.“本地用户和组”,点“用户”项.然后在右边空白处点右键,选择“新用户”将打 ...

  9. hibernate与mybatis的区别和应用场景

    mybatis 与 hibernate 的区别和应用场景(转) 1    Hibernate : 标准的ORM(对象关系映射) 框架: 不要用写sql, sql 自动语句生成: 使用Hibernate ...

  10. UVa 11292 勇者斗恶龙

    https://vjudge.net/problem/UVA-11292 题意:有n条任意个头的恶龙,你希望雇一些其实把它杀死.一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币 ...