To sort your array of objects you:
- setup
NSSortDescriptor
- use names of your variables as keys to setup descriptor for sorting plus the selector to be executed on those keys
- get the array of descriptors using
NSSortDescriptor
that you've setup
- sort your array based on those descriptors
Here are two examples, one using NSDictionary
and NSString/NSNumber
values sorting onNSNumber
, the other one using custom class with sorting on two NSString
fields.
Follow Sorting and Filtering NSArray Objects in Cocoa programming topics to see more examples and explanation.
Example:
This was done on GNUStep it should work the same on Cocoa - the code is exactly the same - I'll try when I sit in front of my Mac:
First example using NSString
and NSNumber
values with sorting on NSNumber
value:
NSString * NAME = @"name";
NSString * ADDRESS = @"address";
NSString * FREQUENCY = @"frequency";
NSString * TYPE = @"type";
NSMutableArray * array = [NSMutableArray array];
NSDictionary * dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Alehandro", NAME, @"Sydney", ADDRESS,
[NSNumber numberWithInt:100], FREQUENCY,
@"T", TYPE, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Xentro", NAME, @"Melbourne", ADDRESS,
[NSNumber numberWithInt:50], FREQUENCY,
@"X", TYPE, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"John", NAME, @"Perth", ADDRESS,
[NSNumber numberWithInt:75],
FREQUENCY, @"A", TYPE, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Fjord", NAME, @"Brisbane", ADDRESS,
[NSNumber numberWithInt:20], FREQUENCY,
@"B", TYPE, nil];
[array addObject:dict];
Sorting part using descriptors with the Frequency field which is NSNumber
:
NSSortDescriptor * frequencyDescriptor =
[[[NSSortDescriptor alloc] initWithKey:FREQUENCY
ascending:YES] autorelease];
id obj;
NSEnumerator * enumerator = [array objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
NSArray * descriptors =
[NSArray arrayWithObjects:frequencyDescriptor, nil];
NSArray * sortedArray =
[array sortedArrayUsingDescriptors:descriptors];
NSLog(@"\nSorted ...");
enumerator = [sortedArray objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
OUTPUT - sorted by Frequency field:
2009-12-04 x[1] {address = Sydney; frequency = 100; name = Alehandro; type = T; }
2009-12-04 x[1] {address = Melbourne; frequency = 50; name = Xentro; type = X; }
2009-12-04 x[1] {address = Perth; frequency = 75; name = John; type = A; }
2009-12-04 x[1] {address = Brisbane; frequency = 20; name = Fjord; type = B; }
2009-12-04 x[1]
Sorted ...
2009-12-04 x[1] {address = Brisbane; frequency = 20; name = Fjord; type = B; }
2009-12-04 x[1] {address = Melbourne; frequency = 50; name = Xentro; type = X; }
2009-12-04 x[1] {address = Perth; frequency = 75; name = John; type = A; }
2009-12-04 x[1] {address = Sydney; frequency = 100; name = Alehandro; type = T; }
Second example with custom class and sorting on two NSString
variables.
Array to sort (see class A
at the bottom):
NSMutableArray * array = [NSMutableArray array];
[array addObject:[[A alloc] initWithFirstName:@"Alehandro"
lastName:@"Xentro"
age:[NSNumber numberWithInt:40]]];
[array addObject:[[A alloc] initWithFirstName:@"John"
lastName:@"Smith"
age:[NSNumber numberWithInt:30]]];
[array addObject:[[A alloc] initWithFirstName:@"John"
lastName:@"Smyth"
age:[NSNumber numberWithInt:25]]];
[array addObject:[[A alloc] initWithFirstName:@"Torro"
lastName:@"Ola"
age:[NSNumber numberWithInt:45]]];
[array addObject:[[A alloc] initWithFirstName:@"Alehandro"
lastName:@"Bento"
age:[NSNumber numberWithInt:41]]];
[array addObject:[[A alloc] initWithFirstName:@"Alehandro"
lastName:@"Axel"
age:[NSNumber numberWithInt:41]]];
The sorting part, sort on lastName then firstName:
NSString * LASTNAME = @"lastName";
NSString * FIRSTNAME = @"firstName";
NSSortDescriptor *lastDescriptor =
[[[NSSortDescriptor alloc]
initWithKey:LASTNAME
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSSortDescriptor *firstDescriptor =
[[[NSSortDescriptor alloc]
initWithKey:FIRSTNAME
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray * descriptors =
[NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil];
NSArray * sortedArray =
[array sortedArrayUsingDescriptors:descriptors];
Print the result:
NSLog(@"\nSorted ...");
enumerator = [sortedArray objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
Result (before and after sorting):
2009-12-04 00:52:16.637 x[11375] Alehandro, Xentro, age:40
2009-12-04 00:52:16.644 x[11375] John, Smith, age:30
2009-12-04 00:52:16.644 x[11375] John, Smyth, age:25
2009-12-04 00:52:16.644 x[11375] Torro, Ola, age:45
2009-12-04 00:52:16.645 x[11375] Alehandro, Bento, age:41
2009-12-04 00:52:16.645 x[11375] Alehandro, Axel, age:41
2009-12-04 00:52:16.645 x[11375]
Sorted ...
2009-12-04 00:52:16.645 x[11375] Alehandro, Axel, age:41
2009-12-04 00:52:16.645 x[11375] Alehandro, Bento, age:41
2009-12-04 00:52:16.645 x[11375] Torro, Ola, age:45
2009-12-04 00:52:16.645 x[11375] John, Smith, age:30
2009-12-04 00:52:16.645 x[11375] John, Smyth, age:25
2009-12-04 00:52:16.645 x[11375] Alehandro, Xentro, age:40
Class A
extends NSObject
- nothing special here:
#import <Foundation/Foundation.h>
@interface A : NSObject
{
NSString * firstName;
NSString * lastName;
NSNumber * age;
}
- (id)initWithFirstName:(NSString*)aFirstName
lastName:(NSString*)aLastName
age:(NSNumber*)anAge;
-(NSString* )description;
+(NSString*)action;
@end
Implementation:
#import <Foundation/Foundation.h>
#import "A.h"
@implementation A
- (id)init
{
return [self initWithFirstName:@"N/A"
lastName:@"N/A"
age:0];
}
- (id)initWithFirstName:(NSString*)aFirstName
lastName:(NSString*)aLastName
age:(NSNumber*)anAge
{
self = [super init];
if (!self) return nil;
firstName = [aFirstName copy];
lastName = [aLastName copy];
age = [anAge copy];
return self;
}
- (void)dealloc
{
[firstName release];
[lastName release];
[age release];
[super release];
}
- (NSString *) description
{
return [NSString stringWithFormat: @"%@, %@, age:%@",
firstName, lastName, age];
}
相关推荐
NSArray排序demo
//取第一页,每页10条按条件查询数据,指定字段,设置分页,设置排序,返回结果为NSArray 排序中 desc 代表 降序,asc代表升序 单个字段排序 如 id desc 多个字段排序 如 id,wheel ascNSString *where = @"name='...
NSArray *arr = [[NSArray alloc] initWithObjects:如果要初始化1000项,可以这样:当然换成swift就超级简单了
官方文档,数组类的最权威文档。各种数组方法的详细说明。英文的。
使用数据结构的排序算法,对NSArray进行排序,包括快速、选择、插入、希尔等四种排序算法
Object C 字符串和数组NSString NSArray操作
NSString NSArray NSDictionary 方法名释义
比较完美的解决了数组越界的问题, 在debug模式下会崩溃 , 在release不会崩溃 , 开发过程中 , 方便调试 , 正式环境中 减少崩溃.
Swift-NSArray数组的使用
Iphone数组一些基础操作 NSArray/NSMutableArrayIphone
OC-NSArray数组的使用Demol
1. 数组的常用处理方式 //--------------------不可变数组 //1....NSString *s1 = @zhangsan; NSString *s2 = @lisi; NSString *s3 = @wangwu;...NSArray *array1 = [[NSArray ...NSArray *array2 = [NSArray arrayWithOb
NSPredicate应用,包括以下几个部分: 1、字符串,正则表达式的匹配; 2、字符串数组NSArray和NSMutableArray的快捷过滤方式; 3、自定义对象数组NSArray和NSMutableArray的快捷过滤方式;
主要介绍了IOS 开发之 NSMutableArray与NSArray 的区别的相关资料,希望通过本文能掌握这部分内容,需要的朋友可以参考下
NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍,有对应的实例代码
Objective-C的数组比C++,Java的数组强大在于,NSArray保存的对象可以是不同的对象。但只能保存对象,int ,char,double等基本数据类型不能直接保存,需要通过转换成对象才能加入数组。
一些NSArray,NSDictionary,NSSet相关的算法知识1
详细的说明和用法 NSArray,NSSet,NSDictionary 以及他们子类的说明
NSArray-Shuffle 用于混洗数组的 NSArray 类别。方法bjl_shuffledArray - ( NSArray *)bjl_shuffledArray 返回值返回一个数组,其中所有元素都被随机排列。 bjl_shuffledArrayWithItemLimit: - ( NSArray *)bjl_...
object-c 适合初学者,主要学习数组,包括固定数组和可变数组,以及创建简单类文件和使用方法