IOS幾種簡單有效的陣列排序方法
//第一種,利用陣列的sortedArrayUsingComparator呼叫 NSComparator ,obj1和obj2指的陣列中的物件
[plain]
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;
};
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
[outputBefore appendFormat:@"];
}
NSLog(@"排序前:%@",outputBefore);
[outputBefore release];
//第一種排序
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
[outputAfter appendFormat:@"];
}
NSLog(@"排序後:%@",outputAfter);
[outputAfter release];
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;
};
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
[outputBefore appendFormat:@"];
}
NSLog(@"排序前:%@",outputBefore);
[outputBefore release];
//第一種排序
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
[outputAfter appendFormat:@"];
}
NSLog(@"排序後:%@",outputAfter);
[outputAfter release];
第二種 排序方法 利用sortedArrayUsingFunction 呼叫 對應方法customSort,這個方法中的obj1和obj2分別是指陣列中的物件。
[plain]
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;
}
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
[outputBefore appendFormat:@"];
}
NSLog(@"排序前:%@",outputBefore);
[outputBefore release];
NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
[outputAfter appendFormat:@"];
}
NSLog(@"排序後:%@",outputAfter);
[outputAfter release];
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;
}
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
[outputBefore appendFormat:@"];
}
NSLog(@"排序前:%@",outputBefore);
[outputBefore release];
NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
[outputAfter appendFormat:@"];
}
NSLog(@"排序後:%@",outputAfter);
[outputAfter release];
第三種 利用sortUsingDescriptors呼叫NSSortDescriptor
[plain]
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" ascending:NO];//其中,price為陣列中的物件的屬性,這個針對陣列中存放物件比較更簡潔方便
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[_totalInfoArray sortUsingDescriptors:sortDescriptors];
[_airListView refreshTable:_totalInfoArray];
[sortDescriptor release];
[sortDescriptors release];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" ascending:NO];//其中,price為陣列中的物件的屬性,這個針對陣列中存放物件比較更簡潔方便
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[_totalInfoArray sortUsingDescriptors:sortDescriptors];
[_airListView refreshTable:_totalInfoArray];
[sortDescriptor release];
[sortDescriptors release];
20120619更新
排序操作,NSArray提供了很多種方法,按照所利用的排序引數,可以簡單分為descriptor,selector,function,和block排序。
陣列排序的時候有sortedArrayUsingComparator方法,這裡的引數為NSComparator,這個類其實是Block所定義的
typedef NSComparisonResult (^NSComparator)(id obj1,id obj2);
所以我們直接用NSComparator myComparator=^(id obj1,id obj2){};
Block其實可以看作JS裡面的(){}();方法以及.Net中的匿名方法,即不需要宣告就直接使用的方法,主要用處包括回撥、事件處理和排序。宣告和實現可以分開,也可以寫在一起:
[plain] view plaincopyprint?NSInteger (^blockExample)(int,NSString*)=^(int i,NSString *s)
{
NSLog(@"%d,%@",i,s);
};
blockExample(42,@"hello");
NSInteger (^blockExample)(int,NSString*)=^(int i,NSString *s)
{
NSLog(@"%d,%@",i,s);
};
blockExample(42,@"hello");
對於NSArray排序或者其他需要Block作為引數的函式來說,可以寫成下面兩種形式:
//宣告和使用
[plain]
NSComparison ( ^ageComparator )( id, id ) = ^( id o1, id o2 ) {
// 比較函式的實現
};
NSArray* objs_sortedByComparator = [objs sortedArrayUsingComparator:ageComparator];
// 不宣告,徹底匿名使用
NSArray* objs_sortedByComparator = [objs sortedArrayUsingComparator:^( id o1, id o2 ) { //...
}];
NSComparison ( ^ageComparator )( id, id ) = ^( id o1, id o2 ) {
// 比較函式的實現
};
NSArray* objs_sortedByComparator = [objs sortedArrayUsingComparator:ageComparator];
// 不宣告,徹底匿名使用
NSArray* objs_sortedByComparator = [objs sortedArrayUsingComparator:^( id o1, id o2 ) { //...
}];
例子:
[plain]
NSArray *array = [NSArray arrayWithObjects:@"71", @"16", @"51", @"104", @"13",@"2",@"1", nil];
array = [array sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2) {
return (arc4random() % 3) - 1;
}];
// NSLog(@"array:%@", array);
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
sortedArray 是排好序的時間物件陣列
// 按時間排序
NSArray *sortedArray = [srcArrary sortedArrayUsingComparator:^(id obj1, id obj2)
{
NSComparisonResult result = [obj1 compare:obj2];
switch(result)
{
case NSOrderedAscending:
return NSOrderedDescending;
case NSOrderedDescending:
return NSOrderedAscending;
case NSOrderedSame:
return NSOrderedSame;
default:
return NSOrderedSame;
} // 時間從近到遠(遠近相對當前時間而言)
}];
NSArray *array = [NSArray arrayWithObjects:@"71", @"16", @"51", @"104", @"13",@"2",@"1", nil];
array = [array sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2) {
return (arc4random() % 3) - 1;
}];
// NSLog(@"array:%@", array);
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
sortedArray 是排好序的時間物件陣列
// 按時間排序
NSArray *sortedArray = [srcArrary sortedArrayUsingComparator:^(id obj1, id obj2)
{
NSComparisonResult result = [obj1 compare:obj2];
switch(result)
{
case NSOrderedAscending:
return NSOrderedDescending;
case NSOrderedDescending:
return NSOrderedAscending;
case NSOrderedSame:
return NSOrderedSame;
default:
return NSOrderedSame;
} // 時間從近到遠(遠近相對當前時間而言)
}];
//隨機排序
[plain]
NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
array = [array sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2) {
return (arc4random() % 3) - 1;
}];
NSLog(@"array:%@", array);
NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
array = [array sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2) {
return (arc4random() % 3) - 1;
}];
NSLog(@"array:%@", array);
文章連結:http://www.2cto.com/kf/201209/155323.html
相關文章
- js宣告陣列的幾種方式簡單介紹JS陣列
- 遍歷陣列的幾種方法陣列
- 陣列去重的幾種方法陣列
- 分割陣列的幾種方法比較陣列
- JS陣列去重的幾種方法JS陣列
- JavaScript陣列合並的幾種方法JavaScript陣列
- JS陣列隨機排序的三種方法JS陣列隨機排序
- ios 遍歷陣列的八種方法iOS陣列
- Awk 陣列排序多種實現方法陣列排序
- 判斷是否是陣列的幾種方法陣列
- 陣列常用方法的簡單封裝陣列封裝
- 親測有效JS中9種陣列去重方法JS陣列
- JavaScript清空陣列元素簡單方法JavaScript陣列
- PHP 判斷陣列是否為空的幾種方法PHP陣列
- 陣列進行排序的方法陣列排序
- iOS筆記之陣列排序iOS筆記陣列排序
- 簡單的排序方法排序
- 陣列去重的幾種方式陣列
- 列舉的幾種使用案例(簡單,進階,switch,單例)單例
- Shell中字串排序的幾種方法字串排序
- Javascript陣列排序sort方法和自定義排序方法JavaScript陣列排序
- ***PHP陣列排序+php二維陣列排序方法(PHP比較器)PHP陣列排序
- iOS 幾種加密方法iOS加密
- iOS開發·必會的演算法操作:字串陣列排序+模型物件陣列排序iOS演算法字串陣列排序模型物件
- php合併陣列的幾種方式PHP陣列
- 陣列去重的幾個方法陣列
- 簡單介紹js 陣列 fill() 填充方法JS陣列
- 陣列的排序陣列排序
- 簡單的幾個排序演算法排序演算法
- JavaScript陣列 幾個常用方法JavaScript陣列
- 簡述幾種常用的排序演算法排序演算法
- iOS常見的幾種加密方法iOS加密
- 陣列去除重複的幾個方法陣列
- 常用的幾個陣列操作方法陣列
- $.each()方法遍歷陣列和物件簡單物件陣列物件
- 陣列去重的六種方法陣列
- javascript中陣列的22種方法JavaScript陣列
- 3種常見的Web前端陣列排序方式!Web前端陣列排序