473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Two dimensional sort

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
6 13977

"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
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
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
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
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
Thanks for all your help.
Alex

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
5
by: Eclectic | last post by:
Hey all, I have been using usort to sort my multi dimensional arrays ... function cmp($a, $b){ if($a == $b){ return 0; } return ($a < $b) ? -1 : 1; }
4
by: Todd | last post by:
I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are...
18
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
4
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
2
by: moondog | last post by:
How do you sort a 2D array? Dim myArray(10000, 9) As String myArray contains 10000 records with 9 fields. I want to sort on the Ninth field. I want to be able to use the sort method, but...
5
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not...
1
by: Totti | last post by:
Hi everyone, I am a newbie to javascript learning it for 2 months now, i am trying to make a sorter who will read english words from the one textbox or datafile and store them in a 2 dimensional...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.