1

我想知道是否可以排序一个NSMutableArray同时保持它NSMutableArray(不是NSArray)。创建一个新的排序数组或重用相同的数组都可以。

- - - - - 编辑 - - - - - - - - -

感谢您的所有回答。

[unsortedList sortUsingSelector:@selector(compareStage:)]; 

对 unsortedList 进行排序。

我对 sorted* Array *UsingSelector 感到困惑。

4

2 回答 2

2

您可以NSMutableArray使用这些方法对原件本身进行排序...

- (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context;
- (void)sortUsingSelector:(SEL)comparator;
- (void)sortUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (void)sortUsingDescriptors:(NSArray *)sortDescriptors;       // NSSortDescriptorSorting
于 2013-11-04T16:20:54.433 回答
0

使用以下代码:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"someKey" ascending:YES];
NSMutableArray *sortedArray = [[unsortedMutableArray sortedArrayUsingDescriptors:@[sortDescriptor]] mutableCopy];
于 2013-11-04T16:10:09.993 回答