NSMutableArray 排序【转】
2011-08-16 16:41
495 查看
(1)直接调用系统的方法排序int
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithInt:20]];
[array addObject:[NSNumber numberWithInt:1]];
[array addObject:[NSNumber numberWithInt:4]];
NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)];
for(inti =0; i < [sortedArraycount]; i++)
{
intx = [[sortedArrayobjectAtIndex:i]intValue];
NSLog(@"######%d\n", x);
}
(2)用descriptor方法
#import<Cocoa/Cocoa.h>
@interface Node: NSObject {
int x;
int y;
int v;
}
@property int x;
@property int y;
@property int v;
- (id)init:(int)tx y:(int)ty v:(int)tv;
@end
@implementation Node
@synthesizex;
@synthesizey;
@synthesizev;
- (id)init:(int)tx y:(int)ty v:(int)tv {
x= tx;
y= ty;
v= tv;
return self;
}
@end
int main(int argc,char*argv[])
{
NSMutableArray *myMutableArray = [[NSMutableArray alloc]init];
Node *n[2];
n[0] = [[Node alloc]init:2 y:1 v:1];
n[1] = [[Node alloc]init:4 y:2 v:2];
[myMutableArray addObject:n[0]];
[myMutableArray addObject:n[1]];
NSSortDescriptor* sortByA = [NSSortDescriptor sortDescriptorWithKey:@"x" ascending:NO];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortByA]];
for(Node *t in myMutableArray) {
NSLog(@"x === %d", t.x);
NSLog(@"y === %d", t.y);
NSLog(@"v === %d", t.v);
}
}
(3)自定义重写方法
/*
Abstract:Constants returned by comparison functions, indicating whether a value is equal to, less than, or greater than another value.
Declaration:enum CFComparisonResult{
kCFCompareLessThan= -1,
kCFCompareEqualTo= 0,
kCFCompareGreaterThan= 1
};
*/
#import <Cocoa/Cocoa.h>
@implementation NSNumber (MySort)
- (NSComparisonResult) myCompare:(NSString *)other {
//这里可以作适当的修正后再比较
int result = ([self intValue]>>1) - ([other intValue]>>1);
//这里可以控制排序的顺序和逆序
return result < 0 ? NSOrderedDescending : result > 0 ? NSOrderedAscending : NSOrderedSame;
}
@end
int main(int argc, char *argv[])
{
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:[NSNumber numberWithInt:20]];
[array addObject:[NSNumber numberWithInt:1]];
[array addObject:[NSNumber numberWithInt:4]];
NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(myCompare:)];
for(int i = 0; i < [sortedArray count]; i++)
{
int x = [[sortedArray objectAtIndex:i]intValue];
NSLog(@"######%d\n", x);
}
}
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithInt:20]];
[array addObject:[NSNumber numberWithInt:1]];
[array addObject:[NSNumber numberWithInt:4]];
NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)];
for(inti =0; i < [sortedArraycount]; i++)
{
intx = [[sortedArrayobjectAtIndex:i]intValue];
NSLog(@"######%d\n", x);
}
(2)用descriptor方法
#import<Cocoa/Cocoa.h>
@interface Node: NSObject {
int x;
int y;
int v;
}
@property int x;
@property int y;
@property int v;
- (id)init:(int)tx y:(int)ty v:(int)tv;
@end
@implementation Node
@synthesizex;
@synthesizey;
@synthesizev;
- (id)init:(int)tx y:(int)ty v:(int)tv {
x= tx;
y= ty;
v= tv;
return self;
}
@end
int main(int argc,char*argv[])
{
NSMutableArray *myMutableArray = [[NSMutableArray alloc]init];
Node *n[2];
n[0] = [[Node alloc]init:2 y:1 v:1];
n[1] = [[Node alloc]init:4 y:2 v:2];
[myMutableArray addObject:n[0]];
[myMutableArray addObject:n[1]];
NSSortDescriptor* sortByA = [NSSortDescriptor sortDescriptorWithKey:@"x" ascending:NO];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortByA]];
for(Node *t in myMutableArray) {
NSLog(@"x === %d", t.x);
NSLog(@"y === %d", t.y);
NSLog(@"v === %d", t.v);
}
}
(3)自定义重写方法
/*
Abstract:Constants returned by comparison functions, indicating whether a value is equal to, less than, or greater than another value.
Declaration:enum CFComparisonResult{
kCFCompareLessThan= -1,
kCFCompareEqualTo= 0,
kCFCompareGreaterThan= 1
};
*/
#import <Cocoa/Cocoa.h>
@implementation NSNumber (MySort)
- (NSComparisonResult) myCompare:(NSString *)other {
//这里可以作适当的修正后再比较
int result = ([self intValue]>>1) - ([other intValue]>>1);
//这里可以控制排序的顺序和逆序
return result < 0 ? NSOrderedDescending : result > 0 ? NSOrderedAscending : NSOrderedSame;
}
@end
int main(int argc, char *argv[])
{
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:[NSNumber numberWithInt:20]];
[array addObject:[NSNumber numberWithInt:1]];
[array addObject:[NSNumber numberWithInt:4]];
NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(myCompare:)];
for(int i = 0; i < [sortedArray count]; i++)
{
int x = [[sortedArray objectAtIndex:i]intValue];
NSLog(@"######%d\n", x);
}
}
相关文章推荐
- IOS学习 NSMutableArray内部元素的排序
- NSMutableArray 排序
- iOS 对NSMutableArray进行排序和过滤
- iOS 对NSMutableArray进行排序和过滤的实例
- NSArray 与 NSMutableArray 的排序
- swift NSMutableArray 排序
- NSArray(不可变数组)、NSMutableArray(可变数组)、数组排序、NSNumber(多态:数值和对象互转)、NSValue(将结构体转换成对象)
- NSMutableArray的双条件或多条件排序
- NSMutableArray 排序
- 排序ARRAY使用NSMUTABLEARRAY
- 关于NSMutableArray的排序
- NSMutableArray 排序
- NSMutableArray 排序 ----- sortedArrayUsingFunction
- OBjective-C:在可变数组NSMutableArray中添加相同对象后,进行自定义的排序方式输出
- 利用 NSSortDescriptor 对 NSMutableArray 排序
- NSMutableArray复杂对象排序
- NSMutableArray 排序
- NSMutableArray复杂对象排序
- IOS学习 NSMutableArray中元素排序
- NSMutableArray 排序