NSMutableDictionary 排序問題


NSMutableDictionary 默認情況下是按字母的順序進行排序的 (a-z)的默認排序
如何自定義排序呢?

第一種,利用數組的sortedArrayUsingComparator調用 NSComparator ,obj1和obj2指的數組中的對象

示例:
//聲明一個數組
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);

//調用sortedArrayUsingComparator排序后
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
	[outputAfter appendFormat:@"%@",str];
}
NSLog(@"排序后:%@",outputAfter);

//調用的排序的方法
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;
};

  第二種 排序方法 利用sortedArrayUsingFunction 調用 對應方法customSort,這個方法中的obj1和obj2分別是指數組中的對象。

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

NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];

NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
	[outputAfter appendFormat:@"];
}
NSLog(@"排序后:%@",outputAfter);

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;
}

  第三種 利用sortUsingDescriptors調用NSSortDescriptor

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" 
															   ascending:NO];//其中,price為數組中的對象的屬性,這個針對數組中存放對象比較更簡潔方便
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[_totalInfoArray sortUsingDescriptors:sortDescriptors];
[_airListView refreshTable:_totalInfoArray];

  字符串的比較模式:

NSComparator cmptr = ^(id obj1, id obj2){
    if([[NSString stringWithFormat:@"%@",obj1] compare:[NSString stringWithFormat:@"%@",obj2] options:NSNumericSearch] > 0)
    {
        return (NSComparisonResult)NSOrderedDescending;
    }
    
    if([[NSString stringWithFormat:@"%@",obj1] compare:[NSString stringWithFormat:@"%@",obj2] options:NSNumericSearch] < 0)
    {
        return (NSComparisonResult)NSOrderedAscending;
    }

    return (NSComparisonResult)NSOrderedSame;
};

  數字比較模式:

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;
}

  本文來自:http://www.gowhich.com/blog/177


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM