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 ...
随机推荐
- Antelope 和Barracuda区别
Antelope是innodb-base的文件格式, Barracude是innodb-plugin后引入的文件格式,同时Barracude也支持Antelope文件格式.两者区别在于: 文件格式 支 ...
- 【netstream】探索数据传输对象1
什么是“从当前流中读取一个字符串.字符串有长度前缀,一次 7 位地被编码为整数.” 来探索一下: 写一段简单的程序: FileStream fs= new FileStream("d:\\q ...
- Underscore 源码
Underscore 源码 作者:韩子迟 What? 不知不觉间,「Underscore 源码解读系列」进入了真正的尾声,也请允许我最后一次 po 下项目的原始地址 https://github.co ...
- About the Storage allocation
It doesn't matter what programming language u use,it's all about the usage of variable---storage man ...
- PHP的会话处理函数session
(๑•ᴗ•๑) PHP Session 变量 当运行一个应用程序时,你会打开它,做些更改,然后关闭它.这很像一次会话.计算机清楚你是谁.它知道你何时启动应用程序,并在何时终止.但是在因特网上,存在一个 ...
- C# testJsonAsXMLNodeAttribute - XML& json & Collections - XmlNode, XmlElement, XmlAttribute,Dictionary,List
testJsonAsXMLNodeAttribute using Newtonsoft.Json; using System; using System.Collections.Generic; us ...
- c++ read
#include <fstream> #include <iostream> int main(void) { ] = {}; ; std::ifstream read(&qu ...
- vim插件介绍
代码补全 http://blog.sina.com.cn/s/blog_a6559d920101acv3.html这个牛逼.************************************** ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
题目链接: http://codeforces.com/contest/575/problem/B 题解: 把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的). ...
- linux进程管理之服务
init进程首先通过initable查看运行级别,然后运行rc.d下面的sysinit,然后调用rc,然后运行rc###连接到init.d下面的服务.自启动. chkconfig命令只是查看和设置服 ...