首页 » iOS 开发 » NSArray的排序方法

NSArray的排序方法

作者: 低调的术木 分类: iOS 开发 发布时间: 2013-03-25 22:08 ė浏览 3,297 次 6没有评论

1.sortedArrayUsingSelector

(按Key值大小对NSDictionary排序)
NSMutableArray *array = [NSMutableArray arrayWithObjects:
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];

NSArray *resultArray = [array sortedArrayUsingSelector:@selector(compare:)];

因为NSDictionary没有compare的排序比较方法,所以需要我们自己写

.h
@interface NSDictionary (compare)
{}

-(NSComparisonResult)compare:(NSDictionary *)otherDictionary;

@end

.m
@implementation NSDictionary (compare)

- (NSComparisonResult)compare: (NSDictionary *)otherDictionary
{
NSDictionary *tempDictionary = (NSDictionary *)self;

NSNumber *number1 = [[tempDictionary allKeys] objectAtIndex:0];
NSNumber *number2 = [[otherDictionary allKeys] objectAtIndex:0];

NSComparisonResult result = [number1 compare:number2];

return result == NSOrderedDescending; // 升序
//    return result == NSOrderedAscending;  // 降序
}

@end

2.sortedArrayUsingComparator

NSMutableArray *array = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];

//    NSArray *resultArray = [array sortedArrayUsingSelector:@selector(compare:)];

NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

NSNumber *number1 = [[obj1 allKeys] objectAtIndex:0];
NSNumber *number2 = [[obj2 allKeys] objectAtIndex:0];

NSComparisonResult result = [number1 compare:number2];

return result == NSOrderedDescending; // 升序
//        return result == NSOrderedAscending;  // 降序
}];

3.sortedArrayUsingDescriptors & sortUsingDescriptors

前者带返回值,是NSArray的方法,排好序的数组是返回值中的数组;
后者不带返回值,是NSMutableArray的方法,是对当前数组自己排序
.h
@interface Person : NSObject
{
NSString  *_name;

NSInteger  _age;
}
@property (nonatomic, retain) NSString  *name;
@property (nonatomic, assign) NSInteger  age;
@end

.m
@implementation Person
@synthesize name = _name;
@synthesize age  = _age;

- (void)dealloc
{
[_name release];

[super dealloc];
}

@end
Person *person1 = [[Person alloc] init];
[person1 setName:@"ABC"];
[person1 setAge:24];

Person *person2 = [[Person alloc] init];
[person2 setName:@"ACB"];
[person2 setAge:22];

Person *person3 = [[Person alloc] init];
[person3 setName:@"ABD"];
[person3 setAge:33];

NSMutableArray *array = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];
[person1 release];
[person2 release];
[person3 release];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"_age" ascending:YES];

NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

for(NSInteger i = 0; i < [tempArray count]; i++)
{
NSLog(@"%@--------%d\n", [[tempArray objectAtIndex:i] name], [[tempArray objectAtIndex:i] age]);
}

//    [array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
//
//    for(NSInteger i = 0; i < [array count]; i++)
//    {
//        NSLog(@"%@--------%d\n", [[array objectAtIndex:i] name], [[array objectAtIndex:i] age]);
//    }

sortDescriptorWithKey:@”_age”,以age作为标准来做比较。


Person *person1 = [[Person alloc] init];
[person1 setName:@"ABCD"];
[person1 setAge:24];

Person *person2 = [[Person alloc] init];
[person2 setName:@"ACBD"];
[person2 setAge:22];

Person *person3 = [[Person alloc] init];
[person3 setName:@"ABDC"];
[person3 setAge:33];

Person *person4 = [[Person alloc] init];
[person4 setName:@"ACDB"];
[person4 setAge:22];

NSMutableArray *array = [NSMutableArray arrayWithObjects:person1, person3, person4, person2, nil];
[person1 release];
[person2 release];
[person3 release];
[person4 release];

NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"_age" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"_name" ascending:YES];

NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];

for(NSInteger i = 0; i < [tempArray count]; i++)
{
NSLog(@"%@--------%d\n", [[tempArray objectAtIndex:i] name], [[tempArray objectAtIndex:i] age]);
}

//    [array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
//
//    for(NSInteger i = 0; i < [array count]; i++)
//    {
//        NSLog(@"%@--------%d\n", [[array objectAtIndex:i] name], [[array objectAtIndex:i] age]);
//    }

NSSortDescriptor *sortDescriptor1 = [NSSortDescriptorsortDescriptorWithKey:@”_age”ascending:YES];

NSSortDescriptor *sortDescriptor2 = [NSSortDescriptorsortDescriptorWithKey:@”_name”ascending:YES];

NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArrayarrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];
这里的NSArray中的第一元素表示首先按照这个元素的升序或者降序进行排序,对于有重复项的,再按照第二元素进行排序,依次进行类推

本文出自 蓝天笔记,转载时请注明出处及相应链接。

本文永久链接: http://ubluesky.com/archives/68

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Ɣ回顶部