新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
OC中的冒泡排序
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è)枚舉類(lèi)型的值,所以要返回一個(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 { //枚舉類(lèi)型的值 // 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
排序
專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)雙流免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了數(shù)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
數(shù)組排序取決于判斷條件,判斷條件決定了排序方式(生序,降序)
IOS為數(shù)組類(lèi)提供類(lèi)排序方法,同時(shí)提供類(lèi)接口讓我們傳遞判斷條件
數(shù)組默認(rèn)排序
[array sortedArrayUsingSelector:<#SEL#>]
[mutableArray sortUsingSelector:<#SEL#>]
@selector,獲取方法名,這個(gè)方法是數(shù)組中元素的方法
默認(rèn)使用升序排列
當(dāng)前題目:OC中的冒泡排序
網(wǎng)址分享:http://www.ef60e0e.cn/article/pjhppg.html