4 回答

TA貢獻1946條經驗 獲得超4個贊
比較方法
要么為對象實現compare方法:
- (NSComparisonResult)compare:(Person *)otherObject { return [self.birthDate compare:otherObject.birthDate];}NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];
NSSortDescriptor(更好)
或者通常更好:
NSSortDescriptor *sortDescriptor;sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthDate" ascending:YES];NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors: @[sortDescriptor]];
通過向陣列添加多個鍵,您可以輕松地按多個鍵進行排序。也可以使用自定義比較器方法??纯?a >文檔。
塊(有光澤!)
從Mac OS X 10.6和iOS 4開始,還可以使用塊進行排序:
NSArray *sortedArray;sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { NSDate *first = [(Person*)a birthDate]; NSDate *second = [(Person*)b birthDate]; return [first compare:second];}];
性能
的-compare:
和基于塊的方法將是相當快一點,一般來講,除了使用NSSortDescriptor
作為后者依賴于KVC。該NSSortDescriptor
方法的主要優點是它提供了一種使用數據而不是代碼來定義排序順序的方法,這使得設置起來很容易,因此用戶可以NSTableView
通過單擊標題行來對其進行排序。

TA貢獻1876條經驗 獲得超6個贊
看NSMutableArray
方法sortUsingFunction:context:
您需要設置一個比較函數,它接受兩個對象(類型Person
,因為您要比較兩個Person
對象)和一個上下文參數。
這兩個對象只是實例Person
。第三個對象是一個字符串,例如@“birthDate”。
此函數返回NSComparisonResult
:NSOrderedAscending
如果PersonA.birthDate
< ,則返回PersonB.birthDate
。NSOrderedDescending
如果PersonA.birthDate
> ,它將返回PersonB.birthDate
。最后,NSOrderedSame
如果PersonA.birthDate
== ,它將返回PersonB.birthDate
。
這是粗糙的偽代碼; 你需要充實一個日期對于另一個日期“更少”,“更多”或“相等”意味著什么(例如比較秒 - 自 - 紀元等):
NSComparisonResult compare(Person *firstPerson, Person *secondPerson, void *context) { if ([firstPerson birthDate] < [secondPerson birthDate]) return NSOrderedAscending; else if ([firstPerson birthDate] > [secondPerson birthDate]) return NSOrderedDescending; else return NSOrderedSame;}
如果你想要更緊湊的東西,你可以使用三元運算符:
NSComparisonResult compare(Person *firstPerson, Person *secondPerson, void *context) { return ([firstPerson birthDate] < [secondPerson birthDate]) ? NSOrderedAscending : ([firstPerson birthDate] > [secondPerson birthDate]) ? NSOrderedDescending : NSOrderedSame;}
如果你這么做的話,內聯也許可以加快一點。

TA貢獻1813條經驗 獲得超2個贊
我在iOS 4中使用塊來完成此操作。不得不將我的數組元素從id轉換為我的類類型。在這種情況下,它是一個名為Score的類,其中包含一個名為points的屬性。
如果數組的元素不是正確的類型,你還需要決定該怎么做,這個例子我剛剛返回NSOrderedSame
,但是在我的代碼中我雖然是個例外。
NSArray *sorted = [_scores sortedArrayUsingComparator:^(id obj1, id obj2){ if ([obj1 isKindOfClass:[Score class]] && [obj2 isKindOfClass:[Score class]]) { Score *s1 = obj1; Score *s2 = obj2; if (s1.points > s2.points) { return (NSComparisonResult)NSOrderedAscending; } else if (s1.points < s2.points) { return (NSComparisonResult)NSOrderedDescending; } } // TODO: default is the same? return (NSComparisonResult)NSOrderedSame;}];return sorted;
PS:這是按降序排序。

TA貢獻1772條經驗 獲得超5個贊
從iOS 4開始,您還可以使用塊進行排序。
對于這個特殊的例子,我假設你的數組中的對象有一個'position'方法,它返回一個NSInteger
。
NSArray *arrayToSort = where ever you get the array from... ;NSComparisonResult (^sortBlock)(id, id) = ^(id obj1, id obj2) { if ([obj1 position] > [obj2 position]) { return (NSComparisonResult)NSOrderedDescending; } if ([obj1 position] < [obj2 position]) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame;};NSArray *sorted = [arrayToSort sortedArrayUsingComparator:sortBlock];
注意:“已排序”數組將自動釋放。
- 4 回答
- 0 關注
- 972 瀏覽
添加回答
舉報