十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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]时候必须intValue // if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1]] == -1){ //这里整体必须有一个返回值,-1,0,1,因为compare的返回值NSComparisonResult是一个枚举类型的值,所以要返回一个值 if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1] options:NSNumericSearch] == 1){ //同上potions NSNumericSearch = 64, [array exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)]; //这里可以用exchangeObjectAtIndex:方法来交换两个位置的数组元素。 } } } 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是按照第一位开始比较的 , //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//大小写敏感的 。 // 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
排序
专注于为中小企业提供成都网站设计、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业双流免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了数千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
数组排序取决于判断条件,判断条件决定了排序方式(生序,降序)
IOS为数组类提供类排序方法,同时提供类接口让我们传递判断条件
数组默认排序
[array sortedArrayUsingSelector:<#SEL#>]
[mutableArray sortUsingSelector:<#SEL#>]
@selector,获取方法名,这个方法是数组中元素的方法
默认使用升序排列