Two dimensional sort

A

Ali Chambers

Hi,

I have a two arrays that I wish to sort. One is the index array (full
of floating point values). The other is a string array:

ARRAY 1 ARRAY 2
------- --------
-1.2 textA
12 textB
23.5 textC
-100.2 textD

I'd like to sort by Array1 but also change the order in Array 2:

ARRAY 1 ARRAY 2
------- --------
23.5 textC
12 textB
-1.2 textA
-100.2 textD

How would I do this in VB.NET?

Thanks,
Alex
 
D

David Lee Conley

Ali Chambers said:
Hi,

I have a two arrays that I wish to sort. One is the index array (full
of floating point values). The other is a string array:

ARRAY 1 ARRAY 2
------- --------
-1.2 textA
12 textB
23.5 textC
-100.2 textD

I'd like to sort by Array1 but also change the order in Array 2:

ARRAY 1 ARRAY 2
------- --------
23.5 textC
12 textB
-1.2 textA
-100.2 textD

How would I do this in VB.NET?

Thanks,
Alex

If the two arrays are parallel (i.e., they have different data, but the data
are related and the arrays are the same size), you sort array 1 and use the
same indexing to sort array 2. For example:

Dim i as Integer
dim dblTemp as double
dim strTemp as string

For i = 0 to (array size -1)
if array1(i) < array(i + 1) then
' Swap the values
dblTemp = array1(i)
array1(i) = array1(i + 1)
array1(i + 1) = dbleTemp
strTemp = array2(i)
array2(i) = array2(i + 1)
array2(i + 1) = strTemp
endif
next i

NOTE: This is NOT a complete sorting algorithm. I'm assuming you already
know that part. If not, do a google search for "selection sort" algorithm.

Dave
 
O

Oenone

Ali said:
I have a two arrays that I wish to sort. One is the index array (full
of floating point values). The other is a string array:
How would I do this in VB.NET?

You need to sort the float values, but whenever you change the order of that
array just make sure that you change the order of the string array at the
same time.

The following code should do what you want; it performs a simple
bubble-sort, sorting the floating point values into descending order and
keeping the associated descriptions synchronised.

\\\
'Create the arrays
Dim values() As Double = {-1.2, 12, 23.5, -100.2}
Dim descriptions() As String = {"textA", "textB", "textC", "textD"}
'Temporary values to help us swap array items
Dim tempValue As Double
Dim tempDescription As String
Dim i As Integer
Dim changeMade As Boolean

'Loop until we're finished sorting
Do
'Reset the changeMade flag
changeMade = False
'Loop through the items in the arrays except for the final item
For i = 0 To UBound(values) - 1

'Does the next item in the array have a larger value than
the current item?
If values(i + 1) > values(i) Then
'Yes, so we'll swap them over...
tempValue = values(i + 1)
values(i + 1) = values(i)
values(i) = tempValue

'Swap the descriptions too so that everything stays
synchronised
tempDescription = descriptions(i + 1)
descriptions(i + 1) = descriptions(i)
descriptions(i) = tempDescription

'Note that we've made a change so that we can try the
sort again.
changeMade = True
End If

Next

'Keep looping until nothing changes.
'Only at this point will we know that the arrays are fully
sorted
Loop Until changeMade = False

'Display the content of the arrays
For i = 0 To UBound(values)
Debug.WriteLine(values(i) & " : " & descriptions(i))
Next
///

Hope that helps,
 
G

Guest

Ali,

Array.Sort can accept 2 arrays in its argument list. It treats one array as
keys and the other as items and sorts them in parallel. This might work for
you.

Kerry Moorman
 
O

Oenone

Kerry said:
Array.Sort can accept 2 arrays in its argument list. It treats one
array as keys and the other as items and sorts them in parallel. This
might work for you.

Ah yes indeed, that's much nicer -- I'll have to remember that.

It sorts the array into ascending value by default, so to get a descending
sort Ali will need to either Reverse() the arrays or implement a comparer --
Reverse() seems like a lot less work.

Good one.

\\\
'Create the arrays
Dim values() As Double = {-1.2, 12, 23.5, -100.2}
Dim descriptions() As String = {"textA", "textB", "textC", "textD"}
Dim i As Integer

'Sort the arrays
Array.Sort(values, descriptions)

'Reverse the arrays so we get descending order
Array.Reverse(values)
Array.Reverse(descriptions)

'Display the content of the arrays
For i = 0 To UBound(values)
Debug.WriteLine(values(i) & " : " & descriptions(i))
Next
///
 
K

Keith Rebello

I would do the following:

Define a structure MyStruct:
Structure MyStruct
ValueFromArray1 as Double
ValueFromArray2 as String
End Structure

Implement IComparable in MyStruct. This is easy since you're concerned
with double types.

Store your 2 arrays in an arraylist (say MyArrayList) consisting of MyStruct
types.
Then all you have to do is:
MyArrayList.Sort

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top