15,960,574 members
5.00/5 (1 vote)
See more:
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.

P.S am Using .NET in case there is a collection type better suited to this.
Posted
Updated 23-Apr-10 0:50am
v2

## Solution 1

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`.

## Solution 2

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:
VB
```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```

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.

v2

## Solution 3

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. :)