NSArray排序 --- sortedArrayUsingDescriptors
来源:互联网 发布:ps淘宝店标制作 编辑:程序博客网 时间:2024/06/05 09:06
- [dataArray sortedArrayUsingFunction: sortDataArrayByModifyFileDate context:NULL];
- NSInteger sortDataArrayByModifyFileDate(id obj1, id obj2, void *context)
- {
- NSString* str1 = (NSString*)obj1;
- NSString* str2 = (NSString*)obj2;
- NSString* dateString1 = [[str1 componentsSeparatedByString:@"$"] objectAtIndex:2];
- NSString* dateString2 = [[str2 componentsSeparatedByString:@"$"] objectAtIndex:2];
- DLog(@">>>>>str1:%@", dateString1);
- DLog(@">>>>str2:%@", dateString2);
- return ([dateString2 compare:dateString1]);
- }
[dataArray sortedArrayUsingFunction: sortDataArrayByModifyFileDate context:NULL];NSInteger sortDataArrayByModifyFileDate(id obj1, id obj2, void *context){NSString* str1 = (NSString*)obj1;NSString* str2 = (NSString*)obj2;NSString* dateString1 = [[str1 componentsSeparatedByString:@"$"] objectAtIndex:2];NSString* dateString2 = [[str2 componentsSeparatedByString:@"$"] objectAtIndex:2];DLog(@">>>>>str1:%@", dateString1);DLog(@">>>>str2:%@", dateString2); return ([dateString2 compare:dateString1]);}
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 usingNSString
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 twoNSString
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:402009-12-04 00:52:16.644 x[11375] John, Smith, age:302009-12-04 00:52:16.644 x[11375] John, Smyth, age:252009-12-04 00:52:16.644 x[11375] Torro, Ola, age:452009-12-04 00:52:16.645 x[11375] Alehandro, Bento, age:412009-12-04 00:52:16.645 x[11375] Alehandro, Axel, age:412009-12-04 00:52:16.645 x[11375]Sorted ...2009-12-04 00:52:16.645 x[11375] Alehandro, Axel, age:412009-12-04 00:52:16.645 x[11375] Alehandro, Bento, age:412009-12-04 00:52:16.645 x[11375] Torro, Ola, age:452009-12-04 00:52:16.645 x[11375] John, Smith, age:302009-12-04 00:52:16.645 x[11375] John, Smyth, age:252009-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];}
@end
http://zani.iteye.com/blog/1144910
- NSArray排序 --- sortedArrayUsingDescriptors
- NSArray中的数据进行排序 ---- sortedArrayUsingDescriptors
- [ios]NSArray中的数据进行排序 ---- sortedArrayUsingDescriptors
- NSArray 排序
- NSArray排序
- NSArray排序
- NSArray排序
- NSArray 排序
- NSArray 排序
- NSArray排序
- NSArray排序
- nsarray排序
- NSArray排序
- NSArray排序
- NSArray排序
- NSArray排序
- NSArray排序
- NSArray、NSMutableArray排序
- 是要做点记录了
- 一个zip文件由这样三个部分组成
- 工具分享:易读文档下载器(同时支持百度/豆丁)
- The_Linux_Kernel_Module_Programming_Guide_2.6中文版
- VC下写入txt的一点总结&&如何获得当前应用程序的目录&&获得系统日期时间
- NSArray排序 --- sortedArrayUsingDescriptors
- mac使用的一点心得(在 finder中显示隐藏文件)
- 计算字符串的相似度--编程之美3.3
- android ListView详解
- 【状态压缩DP】POJ 1170
- 临时变量
- Struts2.1-WildCard(通配符)
- Java第七课 Java的多线程程序进程和线程的概念,实现多线程的两种方式,线程同步的原理,线程的死锁,运用wait和notify来实现producer - consumer关系,线程终止的两种情况。
- 使用jQuery和CSS3实现的超炫3D画廊特效