iphone - 如何从另一个已排序的数组中获取索引数组?

标签 iphone objective-c nsarray

我想按数组的对象对数组进行排序,然后获取索引,如下所示:

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects: @"3", @"2", @"1", @"0", @"1", @"2", nil];

我希望对象的索引按升序排列。在这里,因为最低值的索引为 3,所以索引将为:3、2、4、1、5、0 或类似的值。

有什么想法吗?

谢谢!

最佳答案

这就是我最终所做的:

//Create a mutable array of the indexes in the myArray (just a list from 0...n)

NSMutableArray *indexes = [[NSMutableArray alloc] init];

for (int i = 0; i < myArray.count; i++){
    [indexes addObject: [NSNumber numberWithInteger:i]];
}

//Create a dictionary with myArray as the objects and the indexes as the keys
NSDictionary *tempDictionary = [NSDictionary dictionaryWithObjects:myArray forKeys:indexes];

//Create an array of myArray's keys, in the order they would be in if they were sorted by the values 
NSArray *sorted = [tempDictionary keysSortedByValueUsingSelector: @selector(compare:)];

关于iphone - 如何从另一个已排序的数组中获取索引数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3614192/

相关文章:

ios - 多个 NSManagedObjectContexts 或单个上下文和 -performBlock

objective-c - 如何禁用 NSLog?

objective-c - 将 View 添加到 subview 会使我的应用程序崩溃

ios - 重新排序 NSDictionary 数组

iphone - 如何在 iPhone 中检测网页 View 页面是否已到达

iphone - iOS - 计算距离、方位角、仰角和相对位置(增强现实)

objective-c - 将 plist 数据读入 NSArray 但得到 null

objective-c - 从 NSArray 获取字符串,使用 NSUserDefaults 保存

iphone - 多次调用 UITableView numberOfRowsInSection

iphone - 如何通过 "pull down and release"手势刷新UIWebView?