2011-11-07 83 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里为好。)

相关问题