我必須通過作爲字符串的對象的屬性對對象數組進行排序。 我該怎麼做?Objective C - 排序字符串數組
17
A
回答
35
你需要使用
-[NSArray sortedArrayUsingSelector:]
或
-[NSMutableArray sortUsingSelector:]
,並通過@selector(compare:)
作爲參數。
3
鏈接這裏是我最終使用;就像一個魅力:
[categoryArray sortedArrayWithOptions:0
usingComparator:^NSComparisonResult(id obj1, id obj2)
{
id<SelectableCategory> cat1 = obj1;
id<SelectableCategory> cat2 = obj2;
return [cat1.name compare:cat2.name options:NSCaseInsensitiveSearch];
}];
SelectableCategory
只是一個@protocol SelectableCategory <NSObject>
定義與它的所有屬性和元素的類別。
5
對於剛剛排序字符串數組:
sorted = [array sortedArrayUsingSelector:@selector(compare:)];
對於排序與鍵 「name」 對象:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(compare:)];
sorted = [array sortedArrayUsingDescriptors:@[sort]];
另外,代替compare:
你可以使用:
caseInsensitiveCompare:
localizedCaseInsensitiveCompare:
相關問題
- 1. Objective-C排序字符串日期數組
- 2. C#合併排序字符串數組
- 3. 排序字符串數組
- 4. 排序字符串數組
- 5. 排序字符串數組
- 6. 排序字符串數組
- 7. 排序字符串數組
- 8. Objective-C Json字符串到PHP數組
- 9. Objective C字符串數組的陣列
- 10. Objective-C的-textfield到字符串數組
- 11. 在字符串數組中排序字
- 12. 排序字符串排列數組
- 13. 重新排序字符串數組
- 14. 排序與c中的字符串數組有關的數組
- 15. 排序字符串數組以 「A A B B C C」 用C
- 16. 排序包含字符串在目標c中的數組
- 17. 合併排序 - 使用Int數組排序字符串數組
- 18. 使用數字C++排序字符串數組
- 19. 排序字符串數組與函數
- 20. 字符串排序在C#
- 21. C# - 排序字符串
- 22. C# - 排序字符串ArrayList
- 23. 將字符串拆分爲字符數組 - Objective-C
- 24. 排序從字符串數組z-A
- 25. python字符串數組排序
- 26. 合併排序字符串數組
- 27. PHP排序字符串數組
- 28. 如何排序字符串數組
- 29. PHP:由字符串鍵數組排序
- 30. 按字符串排序數組
的可能重複的[在assending順序排序陣列..?](http://stackoverflow.com/questions/5816017/sort-array-in-assending-order) – Vladimir 2011-04-28 15:23:34