如何对数组排序但保持原始索引? [英] How do I sort an array but maintain the original index?

查看:204
本文介绍了如何对数组排序但保持原始索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到的情况是,我需要数组中前N个项目,对这些项目进行处理,然后以完全相同的索引重新插入(或更新)它们.我意识到最简单的方法是将一个数组排序为1,然后选择N个数量最高的元素,但是我发现我的逻辑有点难于实现,这就是我的难题所在.

问题:

如果原始集合/数组P是...

P()
索引----> 0 1 2 3 4
元素----> 5 6 16 14 2

N = 2(即我想要前2个最高元素)

我可以对P()进行一次排序并得到Q1,但我想按如下方式维护索引. *这是我的问题所在
(按元素和维护的索引排序)
索引----> 4 0 1 3 2
元素----> 2 5 6 14 16

然后...
选择前2个(因为N = 2)来创建Q2.

索引----> 3 2
元素----> 14 16

然后确保索引始终按升序排序I
按索引排序以拥有

索引----> 2 3
元素----> 16 14

在元素上工作,例如

将16更改为17,以使Q2(2)= 17(*-新数据),将14更改为19,以使Q2(3)= 19

然后将索引处的Q1值设置为Q2并进行排序,即
Q1(2)= Q2(2)和Q1(3)= Q2(3)使得
Q1()是
索引----> 0 1 2 3 4
元素----> 5 6 17 * 19 * 2

我不知道使用键是否会有所帮助,甚至不知道如何实现使用键的集合类型,如果可以的话,我试图避免使用并行数组.因此,如果您有任何坦率的建议,意见,信息或正确方向的指导,请与我们分享;我们将不胜感激.

非常感谢您的帮助和宝贵的时间

PS是在使用.NET的情况下,以防存在更适合于此的收集类型.

Hi all,

I''ve landed in a situation where i need the top N number of items in an array, work on those items and reinsert (or update) them back at the exact same indices. I realized that the easiest way would be to sort an array 1st then pick N number of the highest elements, but I find my logic below a little difficult to implement which is where my dilemma is.

Question:

if the original collection/array P were ...

P()
Index ----> 0 1 2 3 4
Element----> 5 6 16 14 2

and N = 2 (i.e I want the top 2 highest elements)

I could sort P() once and get Q1 but I want to maintain the index as below. *this is where my problem is
(sorted by elements and index maintained)
Index ----> 4 0 1 3 2
Element----> 2 5 6 14 16

then...
pick the top 2 (since N = 2) to create Q2.

Index ----> 3 2
Element---->14 16

then to ensure that index is always sorted ascending I
Sort by index to have

Index ----> 2 3
Element----> 16 14

work on the elements, for instance

change 16 to 17 so that Q2(2) = 17 ( * - new data), change 14 to 19 so that Q2(3) = 19

then after that set the values of Q1 at the index to Q2 and sort i.e.
Q1(2) = Q2(2) and Q1(3) = Q2(3) such that
Q1() is
Index ----> 0 1 2 3 4
Element----> 5 6 17* 19* 2

I don''t know whether using keys would help or even how to implement a collection type that uses keys and I am trying to avoid using a parallel array if i can. So please if you have any candid advice, opinions, information or pointers in the right direction kindly share; it would be very much appreciated.

Many thanks for your help and your time

P.S am Using .NET in case there is a collection type better suited to this.

推荐答案

有了您提供的少量信息,我可以做到最好建议您考虑使用Dictionary对象而不是Array.
With the pitiful amount of info you''ve provided, the best I can suggest is that you look into using a Dictionary object instead of an Array.


有很多方法可以解决此问题.

最简单的是只包含一个带有数字的数组.然后,保留一个代表查询的数组,在其中存储最大数字的索引.只需要编写一个简单的for语句即可搜索并找到两个最大数字的索引..

像这样的东西:
There are a lot of ways that you could approach this.

The simplest is to just have a single array with the numbers. Then, keep an array representing query that you store the index of the max numbers in. You would just have to write a simple for statement to search through and find the index of the two largest numbers..

Something like this:
Dim newArray(4) As Integer
newArray(0) = 5
newArray(1) = 6
newArray(2) = 16
newArray(3) = 14
newArray(4) = 2

Dim q2(1) As Integer
q2(0) = -1 'Will store the index of the largest number
q2(1) = -1 'Will store the index of the second largest number

For i = 0 To newArray.Length - 1
    If q2(0) = -1 Then
         q2(0) = i
     ElseIf newArray(i) >= newArray(q2(0)) Then
         q2(1) = q2(0)
         q2(0) = i
     ElseIf newArray(i) >= newArray(q2(1)) Then
         q2(1) = i
     End If
Next



此时,使用您提供的值q2(0) = 2q2(1) = 3或数组中两个最高数字的索引.如果要确保根据索引顺序修改数字,例如newArray(2) = 14newArray(3) = 16会给出q2(0) = 3q2(1) = 2的话,您也始终可以选择对q2进行排序. >
然后,要修改数字,只需修改newArray(q2(0)).

如果您真的想进行排序并想要跟踪原始索引,则很有可能必须使用多维数组.



At this point, with the values you gave, q2(0) = 2 and q2(1) = 3 or the indexes of the two highest numbers in the array. You can always choose to sort q2 too, if for instance newArray(2) = 14 and newArray(3) = 16 which would give q2(0) = 3 and q2(1) = 2 if you wanted to make sure that you were modifying the numbers based on their index order.

Then, to modify the numbers, you just modify newArray(q2(0)).

If you really wanted to do sorting and want to keep track of the original index, you will most likely have to use a multi-dimensional array.


对我来说,这看起来像是一个主要的候选对象一点LINQ.

如果您不熟悉LINQ,请查看 [ ^ ]文章其中有一些将LINQ与数组配合使用的示例.

祝你好运. :)
To me this looks like a prime candidate for a little bit of LINQ.

If you are not familiar with LINQ, take a look at this[^] article which has some examples of using LINQ with Arrays.

Good luck. :)


这篇关于如何对数组排序但保持原始索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆