Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a 2 dimensional array (almost Complete) 3

Status
Not open for further replies.

lazytrucker

Programmer
Aug 4, 2004
39
GB
Almost got this sussed but cant get over the last hurdle, trying to sort both dimensions of the array at once.

Unsorted Output:
Number:= 1 Cost:= 375.20
Number:= 2 Cost:= 375.20
Number:= 3 Cost:= 537.30
Number:= 4 Cost:= 537.30
Number:= 5 Cost:= 45.00
Number:= 6 Cost:= 45.00
Number:= 7 Cost:= 45.00
Number:= 8 Cost:= 45.00

Code:
For i=1 to l
For j=i+1 to l
if arr(1,i)>arr(1,j) then
TemporalVariable=arr(1,i)
arr(1,i)=arr(1,j)
arr(0,i)=j
arr(1,j)=TemporalVariable
end if
next
next

Sorted Output:
Number:= 5 Cost:= 45.00
Number:= 6 Cost:= 45.00
Number:= 7 Cost:= 45.00
Number:= 8 Cost:= 45.00
Number:= 7 Cost:= 375.20
Number:= 8 Cost:= 375.20
Number:= 7 Cost:= 537.30
Number:= 8 Cost:= 537.30


As you can see the output has sorted the cost but not the numbers, which have to relate to the correct price.
If this line is taken out the loop arr(0,i)=j then the numbers simply run from 1 to 8.

Is there a way of getting the two dimensions to always relate to each other?

Or can anyone think of some extra conditioning in order to match up the two dimensions?

Cheers LazyTrucker.
 
Why not just use two temp variables or a small temp array?
Code:
Dim TemporalVariable(1)

For i=1 to l  
   For j=i+1 to l  
      if arr(1,i)>arr(1,j) then
          TemporalVariable(0)=arr(0,i)
          TemporalVariable(1)=arr(1,i)
          arr(1,i)=arr(1,j)
          arr(0,i)=arr(0,j)
          arr(0,j)=TemporalVariable(0)
          arr(1,j)=TemporalVariable(1)
     end if  
   next
next

Same concept as single dimension and would be posible to extend it to an unknown number of dimensions:
Code:
Function Sort(arrToSort,indexToSortOn)
   'assumes row index is second index
   '   and field index is first index
   Dim tempVar

   Dim i, j
   For i = 1 to UBound(arrToSort,2)
      For j = i + 1 to UBound(arrToSort,2)
         If arrToSort(indexToSortOn,i) > arrToSort(indexToSortOn,j) Then
            For q = 0 to UBound(tempvar)
               tempVar = arrToSort(q,i)
               arrToSort(q,i) = arrToSort(q,j)
               arrToSort(q,j) = tempVar
            Next
         End If
      Next
   Next
End Function


If you plan on sorting a great deal of data you may want to look into another method of sorting, such as implementing a quicksort (or even a radix sort for floats).

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Need an expensive ASP developer in the North Carolina area? Feel free to let me know.


 
Thanks Tarwn,

That certainly helped me out. Although there seems to be a slight mistake in the code. Should be:

Function Sort(arrToSort,indexToSortOn)
'assumes row index is second index
' and field index is first index
Dim tempVar

Dim i, j
For i = 1 to UBound(arrToSort,2)
For j = i + 1 to UBound(arrToSort,2)
If arrToSort(indexToSortOn,i) > arrToSort(indexToSortOn,j) Then
For q = 0 to UBound(arrToSort, 1)
tempVar = arrToSort(q,i)
arrToSort(q,i) = arrToSort(q,j)
arrToSort(q,j) = tempVar
Next
End If
Next
Next
End Function


Mighty
 
The (antiquated) search facility here (Tek-Tips) should be able to find several to numerous sort procedures. There is at least one excellent treatsie on the whole concept of sorting, which is well wort the time it takes to find (and --of course-- absorb).



MichaelRed


 
Mighty - thanks for catching that, i probably wrote it on the fly (can't even remember it's been so long) and I seethe error now but I was probably thinking 2 lines ahead when I was writing it :p

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top