2011-11-07 56 views
0

我有一個包含UIButtons的NSMutableArray。我給每個按鈕一個獨特的 標籤。這些按鈕將以隨機順序添加到數組中,但我想稍後對其進行排序,以便按鈕按照其標籤升序排列。Objective-c:如何使用標籤對UIButtons的NSMutableArray進行排序

感謝您的幫助!

+0

如果問題解決了,你應該檢查你的問題的解決,選擇一個答案以幫助未來的人有同樣的問題:) –

回答

6

Yeap。

試試這個:

NSSortDescriptor *ascendingSort = [[NSSortDescriptor alloc] initWithKey:@"tag" ascending:YES]; 
NSSortDescriptor *descendingSort = [[NSSortDescriptor alloc] initWithKey:@"tag" ascending:NO]; 
NSArray *sortedArray = [someArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:ascendingSort]]; 
+0

很明顯,您需要使用ascendingSort或降序NSSortDescriptor –

2

可以使用NSSortDescriptor來排序的NSArray。

NSArray *unsortedArray = ... 
NSArray *sortedArray = [unsortedArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]]; 
1

有多個「sortedArrayUsing ...」函數。使用最符合您需求的產品。

(順便說一句,記住這一點,因爲NSMutableArray的是NSArray中的一個子類,所有的NSArray的方法適用於NSMutableArray裏爲好。)

相關問題