i os 排序
来源:互联网 发布:ubuntu怎么安装iso软件 编辑:程序博客网 时间:2024/09/21 08:57
(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(int i = 0; i < [sortedArray count]; i++)
{
int x = [[sortedArray objectAtIndex: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
@synthesize x;
@synthesize y;
@synthesize v;
- (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);
}
}
(4)
+自己开发中的一点:
NSInteger sectionNumber = [_collation sectionForObject:friendcollationStringSelector:@selector(fullname)];
NSArray *sortedFriendArrayForSection = [_collation sortedArrayFromArray:FriendArrayForSectioncollationStringSelector:@selector(fullname)];
- i os 排序
- i OS UT coverage
- iOS 7
- i os MD5 加密
- i os UIScrollView
- i os sqlite
- i os 定位服务
- i os 照相 图片库
- i os 获取相片
- i os 正则表达式
- i os 远程推送
- 排序I
- 排序I
- i os 同步 异步 下载
- i os 后台GPS 定位
- i os 打开其他app
- i os NSRunloop 的使用
- i os 数据库序列化
- java+mysql对于表情符的处理
- hdu 4046 Panda
- Android中的跨进程通信方法实例及特点分析(一):AIDL Service
- ubuntu 14.04 x86_64编译安装Skyeye1.3.5RC1的心得
- 每周学点js_<a>的href和onlick
- i os 排序
- 常用有机肥有哪几种?应用前应如何处理?如何用作基肥?能不能分别介绍?
- 开发环境搭建--准备
- ZOJ_2671_Cryptography_线段树
- 2014年双11抢货宝典-双11促销商品价格对比清单---妈妈再也不用担心我成为剁手党啦
- linux-0.11内核中任务的堆栈切换
- 第11周项目4-特殊三位数
- HDU 1532 Drainage Ditches(最大流)
- 多个产品的比较问题