您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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,获取方法名,这个方法是数组中元素的方法
默认使用升序排列
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。