您好,登錄后才能下訂單哦!
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"12",@"84", @"35", @"70", @"85", @"99", nil]; NSInteger count = [array count]; for (int i = 0; i < count; i++) { for (int j = 0; j < count - i - 1; j++) { // if ([[array objectAtIndex:j] intValue] > [[array objectAtIndex:(j + 1)] intValue]) { //這里在用[array objectAtIndex:j]時(shí)候必須intValue // if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1]] == -1){ //這里整體必須有一個(gè)返回值,-1,0,1,因?yàn)閏ompare的返回值NSComparisonResult是一個(gè)枚舉類型的值,所以要返回一個(gè)值 if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1] options:NSNumericSearch] == 1){ //同上potions NSNumericSearch = 64, [array exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)]; //這里可以用exchangeObjectAtIndex:方法來(lái)交換兩個(gè)位置的數(shù)組元素。 } } } for (NSString *i in array) { NSLog(@"%@", i); } NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"12",@"84", @"35", @"70", @"85", @"99", nil]; [array1 sortUsingSelector:@selector(compare:)]; NSLog(@"%@", array); //compare方法 // - (NSComparisonResult)compare:(NSString *)aString 這李返回的NSComparisonResult是按照第一位開(kāi)始比較的 , //NSComparisonResult // These constants are used to indicate how items in a request are ordered. // // enum { //枚舉類型的值 // NSOrderedAscending = -1, // NSOrderedSame, // NSOrderedDescending // }; // typedef NSInteger NSComparisonResult; // Search and Comparison Options enum { NSCaseInsensitiveSearch = 1, NSLiteralSearch = 2, NSBackwardsSearch = 4, NSAnchoredSearch = 8, NSNumericSearch = 64, NSDiacriticInsensitiveSearch = 128, NSWidthInsensitiveSearch = 256, NSForcedOrderingSearch = 512, NSRegularExpressionSearch = 1024 }; // Constants // NSCaseInsensitiveSearch//大小寫(xiě)敏感的 。 // A case-insensitive search. // Available in iOS 2.0 and later. // Declared in NSString.h. // // NSLiteralSearch// // Exact character-by-character equivalence. // Available in iOS 2.0 and later. // Declared in NSString.h. // // NSBackwardsSearch //從后往前比較 // Search from end of source string. // Available in iOS 2.0 and later. // Declared in NSString.h. // // NSAnchoredSearch // Search is limited to start (or end, if NSBackwardsSearch) of source string. // Available in iOS 2.0 and later. // Declared in NSString.h. // // NSNumericSearch //交換比較 // Numbers within strings are compared using numeric value, that is, Name2.txt < Name7.txt < Name25.txt. // Numeric comparison only applies to the numerals in the string, not other characters that would have meaning in a true number such as a negative sign or a decimal point. // This option only applies to compare methods, not find. // Available in iOS 2.0 and later. // Declared in NSString.h. // // NSDiacriticInsensitiveSearch // Search ignores diacritic marks. // For example, ‘’ is equal to ‘o’. // Available in iOS 2.0 and later. // Declared in NSString.h. // // NSWidthInsensitiveSearch // Search ignores width differences in characters that have full-width and half-width forms, as occurs in East Asian character sets. // For example, with this option, the full-width Latin small letter 'a' (Unicode code point U+FF41) is equal to the basic Latin small letter 'a' (Unicode code point U+0061). // Available in iOS 2.0 and later. // Declared in NSString.h. // // NSForcedOrderingSearch // Comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal. // This option gives stability when sorting. For example, “aaa” is greater than "AAA” if NSCaseInsensitiveSearch is specified. // Available in iOS 2.0 and later. // Declared in NSString.h. // // // NSRegularExpressionSearch // The search string is treated as an ICU-compatible regular expression. If set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch. You can use this option only with the rangeOfString:... methods and
排序
數(shù)組排序取決于判斷條件,判斷條件決定了排序方式(生序,降序)
IOS為數(shù)組類提供類排序方法,同時(shí)提供類接口讓我們傳遞判斷條件
數(shù)組默認(rèn)排序
[array sortedArrayUsingSelector:<#SEL#>]
[mutableArray sortUsingSelector:<#SEL#>]
@selector,獲取方法名,這個(gè)方法是數(shù)組中元素的方法
默認(rèn)使用升序排列
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。