4
A
回答
6
像這樣(未經):
descriptor = [[[NSSortDescriptor alloc]
initWithKey:@"status"
ascending:YES
selector:@selector(customStatusCompare:)] autorelease];
@interface NSNumber (CustomStatusCompare)
- (NSComparisonResult)customStatusCompare:(NSNumber*)other;
@end
@implementation NSNumber (CustomStatusCompare)
- (NSComparisonResult)customStatusCompare:(NSNumber*)other {
NSAssert([other isKindOfClass:[NSNumber class]], @"Must be a number");
if ([self isEqual:other]) {
return NSOrderedSame;
}
else if (... all your custom comparison logic here ...)
}
}
2
使用自定義比較器或選擇器。 NSSortDescriptor
有一些方法你應該看看。從NSSortDescriptor Class Reference:
+ sortDescriptorWithKey:ascending:selector:
– initWithKey:ascending:selector:
+ sortDescriptorWithKey:ascending:comparator:
– initWithKey:ascending:comparator:
你可能會遇到的問題,如果你傳遞這些類型的排序描述符的一個核心數據讀取請求時,雖然。
相關問題
- 1. NSSortDescriptor自定義字母數字順序
- 2. NSFetchedResultsController的自定義NSSortDescriptor
- 3. 定義自定義排序順序
- 4. 類定義自定義排序順序
- 5. NSSortDescriptor和NSFetchedResultsController的自定義選擇器
- 6. 複雜對象上的自定義NSSortDescriptor
- 7. 使用自定義模型綁定器綁定非順序列表
- 8. 用SimplePie自定義排序順序
- 9. 自定義字母順序
- 10. 自定義整理順序
- 11. 使用NSSortDescriptor排序
- 12. 表中的自定義排序順序
- 13. OrientDB:OIndexNotUnique的自定義排序順序
- 14. WordPress的 - 自定義排序順序
- 15. 使用自定義排序順序的Multikey`table/sortrows`
- 16. 非常自定義的listview行?
- 17. UICollectionView非常慢的自定義佈局
- 18. 自定義MYSQL自定義排序順序
- 19. 使用自定義異常
- 20. SQLALchemy自定義整數排序順序
- 21. ASPxPivotGrid刪除自定義排序/順序
- 22. ArangoDB自定義排序順序
- 23. Rails ActiveRecord 4.2自定義排序順序
- 24. 自定義排序順序PHP + MySQL
- 25. 自定義XML排序順序?
- 26. Perl:自定義排序順序?
- 27. Android:AutoCompleteTextView - 如何自定義排序順序
- 28. GetStream中的自定義活動順序
- 29. 輸出中的自定義行順序
- 30. Linq OrderBy使用自定義排序順序
謝謝,沒關係,但是您是否也可以幫助您完成「您所有的自定義比較邏輯」。我是一個新手... –
這是一個NSNumber,所以你需要'integerValue'來獲得一個簡單的整數。將它們與<, = and >進行比較,並根據您希望如何排序它們來返回「NSOrderedDescending」,「NSOrderedSame」或「NSOrderedAscending」。在你的情況下,如果'[self integerValue]'是1,那麼它總是'NSOrderedAscending'(因爲我們已經檢查了相等性)。如果它是0,那麼它總是'NSOrderedDescending'。如果它是2,那麼你需要檢查另一個值來確定返回哪個值。 –
無法獲取描述符以在類別中調用(customStatusCompare :)。 –