學(xué)習(xí)文章
- NSArray排序方法講解
- “sortedArrayHint” method of NSArray class, what is the purpose of using, and how is it used?
- Sorting using selectors and functions
簡(jiǎn)單講解
NSArray的排序方法:
- sortedArrayUsingFunction:context:
– sortedArrayUsingFunction:context:hint:
– sortedArrayUsingDescriptors:
– sortedArrayUsingSelector:
- sortedArrayUsingComparator:
- sortedArrayWithOptions:usingComparator:
以上方法可以分為四類,Function,Descriptor,Selector,Comparator,最簡(jiǎn)單易用的應(yīng)該是Comparator,用一個(gè)Block非常方便的解決大多數(shù)排序,強(qiáng)烈推薦,其他方法稍微麻煩一些,大家了解一下.下面我們分類講解一下.
Function排序
首先,自己按照參數(shù)要求寫排序方法,如:
NSInteger intSort(id num1, id num2, void *context) {
int v1 = [num1 intValue];
int v2 = [num2 intValue];
if (v1 < v2) {
return NSOrderedAscending;
} else if (v1 > v2) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
然后調(diào)用即可
NSArray *numberArray = @[@2, @4, @12, @1, @9];
NSArray *funcitonSortedArray = [numberArray sortedArrayUsingFunction:intSort context:NULL];
NSLog(@" funcitonSortedArray = %@",funcitonSortedArray);
打印信息:
funcitonSortedArray = (
1,
2,
4,
9,
12
)
Function排序還有一個(gè)方法 – sortedArrayUsingFunction:context:hint:,這個(gè)方法用在這樣的情景:
假設(shè)你有一個(gè)經(jīng)常要排序的大數(shù)組,即使輕微的改變(比如添加或刪除一個(gè)元素),也需要重新對(duì)數(shù)組排序,這樣排序成本很高.這時(shí),我們就可以用此方法.
首先,[anArray sortedArrayHint],此方法應(yīng)該被已排序好的數(shù)組調(diào)用,來(lái)獲得一個(gè)私有的data來(lái)加速輕微改變的數(shù)組的排序.
下面是簡(jiǎn)單用法,由于數(shù)組不夠大,所以,體現(xiàn)不出此方法的優(yōu)勢(shì).
NSInteger alphabeticSort(id string1, id string2, void *reverse)
{
if (*(BOOL *)reverse == YES) {
return [string2 localizedCaseInsensitiveCompare:string1];
}
return [string1 localizedCaseInsensitiveCompare:string2];
}
- (void)hintFunctionDemo {
NSMutableArray *anArray =
[NSMutableArray arrayWithObjects:@"aa", @"ab", @"ac", @"ad", @"ae", @"af", @"ag",
@"ah", @"ai", @"aj", @"ak", @"al", @"am", @"an", @"ao", @"ap", @"aq", @"ar", @"as", @"at",
@"au", @"av", @"aw", @"ax", @"ay", @"az", @"ba", @"bb", @"bc", @"bd", @"bf", @"bg", @"bh",
@"bi", @"bj", @"bk", @"bl", @"bm", @"bn", @"bo", @"bp", @"bq", @"br", @"bs", @"bt", @"bu",
@"bv", @"bw", @"bx", @"by", @"bz", @"ca", @"cb", @"cc", @"cd", @"ce", @"cf", @"cg", @"ch",
@"ci", @"cj", @"ck", @"cl", @"cm", @"cn", @"co", @"cp", @"cq", @"cr", @"cs", @"ct", @"cu",
@"cv", @"cw", @"cx", @"cy", @"cz", nil];
// note: anArray is sorted
NSData *sortedArrayHint = [anArray sortedArrayHint];
[anArray insertObject:@"be" atIndex:5];
// sort with a hint
BOOL reverseSort = NO;
NSArray *sortedArray = [anArray sortedArrayUsingFunction:alphabeticSort
context:&reverseSort
hint:sortedArrayHint];
NSLog(@"hintFunctionSortedArray = %@",sortedArray);
}
打印信息:
hintFunctionSortedArray = (
aa,
ab,
ac,
ad,
ae,
af,
ag,
ah,
ai,
aj,
ak,
al,
am,
an,
ao,
ap,
aq,
ar,
as,
at,
au,
av,
aw,
ax,
ay,
az,
ba,
bb,
bc,
bd,
be,
bf,
bg,
bh,
bi,
bj,
bk,
bl,
bm,
bn,
bo,
bp,
bq,
br,
bs,
bt,
bu,
bv,
bw,
bx,
by,
bz,
ca,
cb,
cc,
cd,
ce,
cf,
cg,
ch,
ci,
cj,
ck,
cl,
cm,
cn,
co,
cp,
cq,
cr,
cs,
ct,
cu,
cv,
cw,
cx,
cy,
cz
)
Selector排序
NSString,NSNumber都有和排序相關(guān)的方法,如NSString有
- (NSComparisonResult)compare:(NSString *)string;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(nullable id)locale;
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
- (NSComparisonResult)localizedCompare:(NSString *)string;
- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;
我們可以直接利用這些方法來(lái)排序
NSArray *alphabeticArray = @[@"b", @"a", @"x", @"o", @"g", @"o"];
NSLog(@"%@",selectorSortedArray);
打印信息:
selectorSortedArray = (
a,
b,
g,
o,
o,
x
)
Descriptor排序
用NSSortDescriptor,它是一個(gè)獲取keyPath的工具,它能根據(jù)keyPath進(jìn)行排序
NSArray *modelArray = @[[Model name:@"LiuDaShuai" age:@26 height:@171],
[Model name:@"XiaoQiu" age:@27 height:@170],
[Model name:@"HaoQuShi" age:@28 height:@172],
[Model name:@"JunGang" age:@24 height:@171],
[Model name:@"KongMing" age:@30 height:@175],
[Model name:@"GaoFuShuai" age:@22 height:@180]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"m_age" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *descriptorSortedArray = [modelArray sortedArrayUsingDescriptors:sortDescriptors];
[descriptorSortedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Model *tmp = obj;
NSLog(@"descriptorSortedArray model = %@", tmp.m_name);
}];
打印信息
descriptorSortedArray model = GaoFuShuai
descriptorSortedArray model = JunGang
descriptorSortedArray model = LiuDaShuai
descriptorSortedArray model = XiaoQiu
descriptorSortedArray model = HaoQuShi
descriptorSortedArray model = KongMing
可以將上面的步驟封裝成類目,便于使用:
NSArray+DescriptorSort.h
#import <Foundation/Foundation.h>
@interface NSArray (DescriptorSort)
- (NSArray*)sortedWithKeyPath: (NSString*)keyPath ascending: (BOOL)ascending;
@end
NSArray+DescriptorSort.m
#import "NSArray+DescriptorSort.h"
@implementation NSArray (DescriptorSort)
- (NSArray*)sortedWithKeyPath: (NSString*)keyPath ascending: (BOOL)ascending {
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc]initWithKey:keyPath ascending:ascending];
return [self sortedArrayUsingDescriptors:@[descriptor]];
}
@end
Comparator排序
最簡(jiǎn)單易用,強(qiáng)烈推薦
NSArray *modelArray = @[[Model name:@"LiuDaShuai" age:@26 height:@171],
[Model name:@"XiaoQiu" age:@27 height:@170],
[Model name:@"HaoQuShi" age:@28 height:@172],
[Model name:@"JunGang" age:@24 height:@171],
[Model name:@"KongMing" age:@30 height:@175],
[Model name:@"GaoFuShuai" age:@22 height:@180]];
NSArray *comparatorSortedArray = [modelArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
Model *model1 = obj1;
Model *model2 = obj2;
return [model1.m_name compare:model2.m_name];
}];
// 打印排序信息
[comparatorSortedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Model *tmp = obj;
NSLog(@"comparatorSortedArray model = %@", tmp.m_name);
}];
打印信息:
comparatorSortedArray model = GaoFuShuai
comparatorSortedArray model = HaoQuShi
comparatorSortedArray model = JunGang
comparatorSortedArray model = KongMing
comparatorSortedArray model = LiuDaShuai
comparatorSortedArray model = XiaoQiu