我有一堆装满数字的NSArrays。有无论如何,我可以以某种方式抓住一个特定的数字,具体如下:从数组中挑出特定数量?
数组中的第三个最高的数字,或数组中的第24个最高的数字?
但我不想只抓住这个数字,我也需要引用它在数组中的索引,如果这可以保留在过程中。
谢谢。
我有一堆装满数字的NSArrays。有无论如何,我可以以某种方式抓住一个特定的数字,具体如下:从数组中挑出特定数量?
数组中的第三个最高的数字,或数组中的第24个最高的数字?
但我不想只抓住这个数字,我也需要引用它在数组中的索引,如果这可以保留在过程中。
谢谢。
数组中第三高的数字或数组中的第24个最高数字?
创建数组的临时副本并对其进行升序排序。
获取索引号为3-1
或24-1
的号码。
我也需要它在阵列中以及
指数的参考现在使用indexOfObject:
或indexOfObjectIdenticalTo:
获得实际的指数。
从我的答案排序numbers
阵列从这里开始Getting Top 10 Highest Numbers From Array?:
NSUInteger wanted = 24 or anything;
NSUInteger count = 0;
NSNumber* previous = nil;
for (NSDictionary* entry in numbers)
{
if (wanted == 1) return entry;
if (previous == nil)
{
// first entry
previous = entry[@"number"];
++ count;
continue;
}
// same number? skip it.
if (entry[@"number"] isEqualTo: previous) continue;
// if we get here, we found a different number we may be interested in
++ count;
if (count == wanted) return entry;
}
// nothing found
return nil;
结果是从以前的问题numbers
排列在一条线上。一组表格@[ @"number" : @<number>, @"parent" : <source array> ]
。
顺便说一句,你可以修改该代码来查找字典中第一个唯一的最高数字。使用相等性测试(使用否定)来递增计数而不是跳过,并将数组中的条目添加到数组==10而不是返回条目。很简单。 –