By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Two dimensional sort

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Ali Chambers" <in**@alexchambers.co.uk> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
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
Nov 21 '05 #2

P: n/a
Ali Chambers wrote:
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,

--

(O)enone
Nov 21 '05 #3

P: n/a
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
"Ali Chambers" wrote:
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

Nov 21 '05 #4

P: n/a
Kerry Moorman wrote:
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
///

--

(O)enone
Nov 21 '05 #5

P: n/a
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.
Nov 21 '05 #6

P: n/a
Thanks for all your help.
Alex

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.