Re: Two dimensional sort

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Ali Chambers" <info@xxxxxxxxxxxxxxxxxx> wrote in message
news:1118320349.942464.38340@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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


.



Relevant Pages

  • 2 Dim Array Sort, on any columns, ascend or descend. Guidance des
    ... descending for each column to be part of the sort. ... is that a sort key is built, an array of keys is sorted, and then used to ... Dim WarnErrMsg As String ...
    (microsoft.public.excel.programming)
  • Re: Counting IP address in firewall log
    ... > array, but I cann't figure out what to do next to count each unique IP. ... > Dim objFSO, objFWFile, arrLine, i ... I also did a REAL cheesy sort using JS for no particular reason then to see ... var rtn; ...
    (microsoft.public.scripting.vbscript)
  • Re: Sorting Arrays via VBS / JScript
    ... > I need to sort the file/Array first so I can do proper reading. ... > ' Now that I have the File in the array. ... dim myIn, myhelpr ... set oSort = WScript.CreateObject ...
    (microsoft.public.scripting.wsh)
  • Re: VBA Sort 2-dimensional array based on 2 column
    ... I pull all of the data into an array then I ... Some of the fields are blank, can that mess up the sort? ... Dim i As Long, j As Long ... 'MergeSort recursively calls itself until we have lists short enough ...
    (microsoft.public.office.developer.vba)
  • Re: VBA Sort 2-dimensional array based on 2 column
    ... a simple bubble sort should work fine. ... If your array is named myArray, and you want to sort by column 5 (say age ... Dim i As Long, j As Long ... Dim ColNr As Long ...
    (microsoft.public.office.developer.vba)