NSArray 数组排序
//方法1,使用自带的比较器//compare是数组自带的比较方法NSArray *array=[NSArray arrayWithObjects:@"3",@"1",@"2", nil];NSArray *array2= [array sortedArrayUsingSelector:@selector(compare:)];NSLog(@"%@",array2);
结果是升序排列
//方式二:使用块完成排NSArray *array = [NSArray arrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil];NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {//这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序NSComparisonResult result = [obj1 compare:obj2];return result;}];NSLog(@"排序后:%@",sortedArray);
方法3:自定义排序
#import <Foundation/Foundation.h>@interface Person : NSObject@property NSString* name;@property int age;-(id)initWithNameAndAge:(NSString*) aName and:(int) aAge;-(NSComparisonResult)comparePersonByAge:(Person *)person;-(NSComparisonResult)comparePersonByName:(Person *)person;@end
#import "Person.h"@implementation Person@synthesize name,age;-(id)initWithNameAndAge:(NSString*) aName and:(int) aAge{if (self=[super init]) {name=aName;age=aAge;}return self;}//自定义排序方法-(NSComparisonResult)comparePersonByAge:(Person *)person{//默认按年龄排序NSComparisonResult result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];//注意:基本数据类型要进行数据转换//如果年龄一样,就按照名字排序//if (result == NSOrderedSame) {// result = [self.name compare:person.name];//}return result;}-(NSComparisonResult)comparePersonByName:(Person *)person{//默认按年龄排序NSComparisonResult result = [ person.name compare:self.name];//注意:基本数据类型要进行数据转换//如果年龄一样,就按照名字排序if (result == NSOrderedSame) {result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];}return result;}- (NSString *)description{return [NSString stringWithFormat:@"%@ %d", name,age];}@end
#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]) {@autoreleasepool {Person *p1 = [[Person alloc]initWithNameAndAge:@" qweasadsasd" and:25];Person *p2 = [[Person alloc]initWithNameAndAge:@"\t1234" and:28];Person *p3 = [[Person alloc]initWithNameAndAge:@"123" and:2];Person *p4 = [[Person alloc]initWithNameAndAge:@"zxc" and:89];Person *p5 = [[Person alloc]initWithNameAndAge:@"123" and:8];NSArray * persons = [NSArray arrayWithObjects:p1,p2,p3,p4,p5,nil];NSArray *sortedArray = [persons sortedArrayUsingSelector:@selector(comparePersonByName:)];NSLog(@"排序后:%@",sortedArray);}return 0;}
方法四:高级排序
NSArray 数组排序的更多相关文章
- OC NSArray数组排序
一.一般排序 // 排序 NSArray *arr = @["]; NSArray *newarr = [arr sortedArrayUsingSelector:@selector(com ...
- iOS之NSArray数组排序
一.数组遍历 除了常用的for和for-in遍历外,系统还提供了三种枚举遍历,对于大量的数据遍历可以使用下列三个方法. - (void)enumerateObjectsUsingBlock:(void ...
- OC中用NSSortDescriptor对象进行数组排序
//创建一个数组 NSArray *array = @[@"one", @"two", @"three", @"four" ...
- Objective C中数组排序几种情况的总结
总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 数组排 ...
- NSSortDescriptor对象进行数组排序
//创建一个数组 NSArray *array = @[@"zhangsan", @"lisi", @"zhonger", @"z ...
- objective-c系列-NSArray
OC数组NSArray 对比 c数组 和 oc数组对象(指针) 定义 int array[10]; NS ...
- NSArray与NSMutableArray 数组与可变数组
1.NSArray 是一个父类,NSMUtableArray是其子类,他们构成了OC的数组.2.NSArray的创建NSArray * array = [[NSArray alloc]initWith ...
- OC NSArray 数组
# OC NSArray 数组 NSArray常用方法 获取数组中第一位元素 array.firstObject 获取数组中最后一个元素 array.lastObject 获取数组中指定索引下标的元素 ...
- Objective-C之NSArray(数组)默认排序与自定义排序
在讲OC中数组的排序之前我先上一段代码,它是简单数组排序的一种方法(也就是元素是字符串或者数据的数组,因为后面要讲元素为类的数组排序) 代码1: NSArray *sortArr4 = [sortAr ...
随机推荐
- Basic Vlan Configure
Basic Vlan CLI Configure Switch>en Switch#conf t Enter configuration commands, one per line. End ...
- hive中简单介绍分区表
所介绍内容基本上是翻译官方文档,比较肤浅,如有错误,请指正! hive中创建分区表没有什么复杂的分区类型(范围分区.列表分区.hash分区.混合分区等).分区列也不是表中的一个实际的字段,而是一个或者 ...
- C++中的struct和class的区别
C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据类型的数据结构了,它已经获取了太多的功能.struct能包含成员函数吗? 能!struct能继承吗? 能!!stru ...
- cocos2dx中的坐标体系
1.UI坐标系和GL坐标系 2.本地坐标与世界坐标 本地坐标是一个相对坐标,是相对于父节点或者你指明的某个节点的相对位置来说的,本地坐标的原点在参考节点的左下角 世界坐标是一个绝对的坐标,是以屏幕的左 ...
- oracle 常用SQL语法手册
Select 用途: 从指定表中取出指定的列的数据 语法: SELECT column_name(s) FROM table_name 解释: 从数据库中选取资料列,并允许从一或多个资料表中,选取一或 ...
- Python求算数平方根和约数
一.求算术平方根 a=0 x=int(raw_input('Enter a number:')) if x >= 0: while a*a < x: a = a + 1 if a*a != ...
- mac mysql安装
一.安装 1.下载软件包直接安装即可: http://rj.baidu.com/soft/detail/25675.html?ald 安装完成后root默认密码为空: 二.修改密码 直接修改密码会提示 ...
- BZOJ 1083: [SCOI2005]繁忙的都市 裸的最小生成树
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1083 代码: #include<iostream> #include< ...
- JSP图片上传 公共工具类
需要jsmartcom_zh_CN.jar支持. 下载地址: http://files.cnblogs.com/simpledev/jsmartcom_zh_CN.rar <%@page imp ...
- Tutorial: Model
What is a model? Across the internet the definition of MVC is so diluted that it's hard to tell what ...