您可以使用NSArray's
sortedArrayUsingFunction:Context:方法为您排序这些.此方法采用可用于比较数组中两个项的函数.
#import <Foundation/Foundation.h>
NSInteger firstNumSort(id str1, id str2, void *context) {
int num1 = [str1 integerValue];
int num2 = [str2 integerValue];
if (num1 < num2)
return NSOrderedAscending;
else if (num1 > num2)
return NSOrderedDescending;
return NSOrderedSame;
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *array = [NSArray arrayWithObjects:@"0 Here is an object",
@"4 Here's another object",
@"25 Let's put 2 here too!",
@"1 What the heck, here's another!",
@"3 Let's put this one right here",
nil];
NSLog(@"Sorted: %@", [array sortedArrayUsingFunction:firstNumSort context:NULL]);
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3272 次 |
最近记录: |