场景:根据需求让数据内的元素有序排列。
下面是具体实例说明。
ArrayObject.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
NSInteger _age;
}
@property (readwrite, copy) NSString *_name;
@property (readwrite, assign) NSInteger _age;
@end
@interface ArrayObject : NSObject
-(void)testSortedArrayUsingComparater;
-(void) testSortArrayUsingSelector;
-(void) testSortedArrayUsingFunction;
-(void) doTestSortUsingDescriptors;
@end
ArrayObject.m
#import "ArrayObject.h"
@implementation Person
@synthesize _name;
@synthesize _age;
- (void)dealloc
{
[_name release];
[super dealloc];
}
@end
@implementation ArrayObject
//第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象
NSComparator cmptr = ^(id obj1, id obj2){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
};
-(void)testSortedArrayUsingComparater
{
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"10",@"13",@"5",@"15",@"8",@"20",@"1",@"5",@"3",@"7",@"45",@"4",@"23",@"",nil];
//排序前
NSMutableString *beforeStr = [NSMutableString new];
for(NSString *str in sortArray)
{
[beforeStr appendFormat:@"%@ ",str];
}
NSLog(@"排序前:%@",beforeStr);
[beforeStr release];
//第一种排序
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSMutableString *afterStr = [NSMutableString new];
for(NSString *str in array){
[afterStr appendFormat:@"%@ ",str];
}
NSLog(@"排序后:%@",afterStr);
[afterStr release];
}
输出结果:
//第二种排序算法 利用sortedArrayUsingSelector调用系统定义的compare:,主要是对字符串的比较
-(void) testSortArrayUsingSelector
{
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"10",@"13",@"5",@"15",@"8",@"20",@"1",@"5",@"3",@"7",@"45",@"4",@"23",@"",nil];
//排序前
NSMutableString *beforeStr = [NSMutableString new];
for(NSString *str in sortArray)
{
[beforeStr appendFormat:@"%@ ",str];
}
NSLog(@"排序前:%@",beforeStr);
[beforeStr release];
//第一种排序
//- (NSComparisonResult)compare:(NSString *)string;//比较字符串的大小
//- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; 比较字符串,按mask的参数进行比较
//mask 参数可以是: NSCaseInsensitiveSearch, NSLiteralSearch, NSNumericSearch.
NSArray *array =[sortArray sortedArrayUsingSelector:@selector(compare:options:)];
NSMutableString *affterStr = [NSMutableString new];
for(NSString *str in array){
[affterStr appendFormat:@"%@ ",str];
}
NSLog(@"排序后:%@",affterStr);
[affterStr release];
}
输出结果:
若把
NSArray *array =[sortArray sortedArrayUsingSelector:@selector(compare:options:)];中<pre name="code" class="objc">compare:options:改为compare:,则输出结果为:
//第三种 排序方法 利用sortedArrayUsingFunction 调用 对应方法customSort,这个方法中的obj1和obj2分别是指数组中的对象。
NSInteger customSort(id obj1, id obj2,void* context){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}
-(void) testSortedArrayUsingFunction
{
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"10",@"13",@"5",@"15",@"8",@"20",@"1",@"5",@"3",@"7",@"45",@"4",@"23",@"",nil];
//排序前
NSMutableString *beforeStr = [NSMutableString new];
for(NSString *str in sortArray){
[beforeStr appendFormat:@"%@ ",str];
}
NSLog(@"排序前:%@",beforeStr);
[beforeStr release];
NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];
NSMutableString *afterStr = [NSMutableString new];
for(NSString *str in array){
[afterStr appendFormat:@"%@ ",str];
}
NSLog(@"排序后:%@",afterStr);
[afterStr release];
}
输出结果:
//第四种 利用sortUsingDescriptors调用NSSortDescriptor
-(void) doTestSortUsingDescriptors
{
Person *person1 = [[Person alloc] init];
[person1 set_name:@"ABCD"];
[person1 set_age:24];
Person *person2 = [[Person alloc] init];
[person2 set_name:@"ACBA"];
[person2 set_age:22];
Person *person3 = [[Person alloc] init];
[person3 set_name:@"ABDC"];
[person3 set_age:33];
NSMutableArray *array = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];
[person1 release];
[person2 release];
[person3 release];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"_name" ascending:YES];//YES降序,NO升序
//这个数组保存的是排序好的对象
NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
for (Person *ii in tempArray) {
NSLog(@"name:%@,age:%ld",ii._name,ii._age);
}
}
@end
输出结果:
降序:ascending:YES
升序:ascending:NO
用数据结构排序算法对NSArray进行排序,项目代码点击链接下载:
http://download.csdn.net/detail/moqj_123/9252059