## 5 replies

Last post Aug 07, 2008 04:24 AM by vit0

• vit0

Member

34 Points

84 Posts

### Help to create the array of indexes

Hi!

I'm coding in VB.NET and I need help in making an array:

I have an array of integers, for example:

dim Arr() as integer
Arr(0) = 26
Arr(1) = 4
Arr(2) = 20
Arr(3) = 12

I have to make another array, that contains indexes of numbers in Arr() ordered by increment.

Result array will contain:

res(0) = 1
res(1) = 3
res(2) = 2
res(3) = 0

• sambeetpatra

Participant

940 Points

227 Posts

### Re: Help to create the array of indexes

```Dim Arr(3) As Integer
Arr(0) = 26
Arr(1) = 4
Arr(2) = 20
Arr(3) = 12

Dim Arr1(Arr.Length) As Integer
Dim lastValue As Integer
Dim currValue As Integer
Dim currIndex As Integer

lastValue = Integer.MinValue

For i As Integer = 0 To Arr.Length - 1
currValue = Arr(0)
For j As Integer = 0 To Arr.Length - 1
If Arr(j) <= currValue And Arr(j) > lastValue Then
currIndex = j
currValue = Arr(j)
End If
Next
Arr1(i) = currIndex
lastValue = Arr(currIndex)
Next

For i As Integer = 0 To Arr.Length - 1
Console.WriteLine(Arr1(i).ToString())
Next```

Sambeet
• vit0

Member

34 Points

84 Posts

### Re: Help to create the array of indexes

Thank you, it works, but not in all cases:

If      Arr(0) = 26
Arr(1) = 4
Arr(2) = 20
Arr(3) = 12
so, the output is right: 1,3,2,0

Now try to change input to:
Arr(0) = 7
Arr(1) = 50
Arr(2) = 10
Arr(3) = 12
the output will be: 2,3,0,0

or if my input is:
Arr(0) = 26
Arr(1) = 50
Arr(2) = 4
Arr(3) = 12
the output will be: 0,0,0,0

Why is it so?

• sambeetpatra

Participant

940 Points

227 Posts

### Re: Help to create the array of indexes

I have modified the logic using bubble sorting. Can you please try this:

```Module Module1

Sub Main()
Dim Arr(3) As Integer
Arr(0) = 26
Arr(1) = 50
Arr(2) = 4
Arr(3) = 12

Dim ArrTemp(Arr.Length - 1) As Integer
Dim Arr1(Arr.Length - 1) As Integer
Dim tempval As Integer

For i As Integer = 0 To Arr1.Length - 1
Arr1(i) = i
ArrTemp(i) = Arr(i)
Next

'Use bubble sort now
For i As Integer = 0 To ArrTemp.Length - 1
For j As Integer = i + 1 To ArrTemp.Length - 1
If ArrTemp(i) > ArrTemp(j) Then
tempval = ArrTemp(j)
ArrTemp(j) = ArrTemp(i)
ArrTemp(i) = tempval

'Also swap the index
tempval = Arr1(j)
Arr1(j) = Arr1(i)
Arr1(i) = tempval

End If
Next
Next

For i As Integer = 0 To Arr.Length - 1
Console.WriteLine(Arr1(i).ToString())
Next

End Sub

End Module```

Sambeet
• DkUltra

Participant

1721 Points

543 Posts

### Re: Help to create the array of indexes

Hear is an easy way to acomplish what is needed

first create a find function and a find variable then do the work.

Public searchNumber As Integer

Public Function findIndex(ByVal i As Integer)

If i = searchNumber Then

Return True

Else

Return False

End If

End Function

Dim yourArray() As Integer = {7, 50, 10, 12}

Dim SortedArray As List(Of Integer) = yourArray.ToList()

SortedArray.Sort()

Dim Indexs(yourArray.Count()) As Integer

Dim index As Integer = 0

For Each i In yourArray

searchNumber = i

Indexs(index) = SortedArray.FindIndex(

index += 1

Next

Hope this helps

DK

• vit0

Member

34 Points

84 Posts