• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Objective-csortarrayint方法(附:comparisonmethods)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

(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);

}

}

 

注:

关于 sortedArrayUsingSelector:@selector(copare:)是默认的排序方法,而你自己可以重写排序方法,比如上面的myCompare


附:

search/find and comparison methods

enum {
NSCaseInsensitiveSearch = 1,
NSLiteralSearch = 2, /* Exact character-by-character equivalence */
NSBackwardsSearch = 4, /* Search from end of source string */
NSAnchoredSearch = 8, /* Search is limited to start (or end, if NSBackwardsSearch) of source string */
NSNumericSearch = 64 /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find */
#if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
,
NSDiacriticInsensitiveSearch = 128, /* If specified, ignores diacritics (o-umlaut == o) */
NSWidthInsensitiveSearch = 256, /* If specified, ignores width differences ('a' == UFF41) */
NSForcedOrderingSearch = 512 /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 */
#if MAC_OS_X_VERSION_10_7 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED
,
NSRegularExpressionSearch = 1024 /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 || __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED */
};




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C类和对象发布时间:2022-07-12
下一篇:
关于Objective-CiVar的一点说明发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap