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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。