Can anyone spot why this simple bubble sort will not act on the simple array I have given it? It is supposed to sort on first column but fails to do so. I'm doing something wrong I'm sure... but cannot find it.
Any help appreciated...
Thanks
[vba]
Option Explicit
Sub testsort()
''Sort a 2 dimensional array on 1 column
''This example sorts a two dimensional array named ArrayName on the first column
''(column 0). The sort is ascending. Reverse the > sign in the fourth row for a
''descending sort. bubble sort
Dim trip() As Variant
Dim u As Long
Dim v As Long
Dim SortColumn1 As Long
Dim Condition1 As Long
Dim i As Long
Dim j As Long
Dim y As Long
Dim t As Variant
'-------------------------------------------------------------------------------
'Read Array
'-------------------------------------------------------------------------------
ReDim trip(1 To 5, 1 To 4)
trip(1, 1) = -0.016667
trip(2, 1) = "Red"
trip(3, 1) = "7:19"
trip(4, 1) = "6:35"
trip(5, 1) = 1
trip(1, 2) = 0.05625
trip(2, 2) = "Blue"
trip(3, 2) = "7:19"
trip(4, 2) = "06:40"
trip(5, 2) = 1
trip(1, 3) = 0.021528
trip(2, 3) = "Red"
trip(3, 3) = "7:19"
trip(4, 3) = "06:52"
trip(5, 3) = 2
trip(1, 4) = -0.004167
trip(2, 4) = "Blue"
trip(3, 4) = "7:19"
trip(4, 4) = "08:20"
trip(5, 4) = 3
'-------------------------------------------------------------------------------
'Confirm array in two different ways before sort
'-------------------------------------------------------------------------------
For u = 1 To UBound(trip, 2)
Sheets("sort1").Cells(u, 1).Value = trip(1, u) 'calculate time
Sheets("sort1").Cells(u, 2).Value = trip(2, u) 'stop
Sheets("sort1").Cells(u, 3).Value = trip(3, u) 'stop_time_now
Sheets("sort1").Cells(u, 4).Value = trip(4, u) 'time
Sheets("sort1").Cells(u, 5).Value = trip(5, u) 'trip number
Sheets("sort1").Cells(u, 6).Value = u 'index 1
Next u
For u = 1 To UBound(trip, 1)
For v = 1 To UBound(trip, 2)
Sheets("alt_sort1").Cells(v, u).Value = trip(u, v)
Sheets("alt_sort1").Cells(v, u + 1).Value = u
Sheets("alt_sort1").Cells(v, u + 2).Value = v
Next v
Next u
'-------------------------------------------------------------------------------
'Sort
'-------------------------------------------------------------------------------
SortColumn1 = 1
For i = LBound(trip, 1) To UBound(trip, 1) - 1
For j = LBound(trip, 1) To UBound(trip, 1) - 1
Condition1 = trip(j, SortColumn1) > trip(j + 1, SortColumn1)
If Condition1 Then
For y = LBound(trip, 2) To UBound(trip, 2)
t = trip(j, y)
trip(j, y) = trip(j + 1, y)
trip(j + 1, y) = t
Next y
End If
Next
Next
'-------------------------------------------------------------------------------
'Confirm in two different ways After Sort
'-------------------------------------------------------------------------------
For v = 1 To UBound(trip, 2)
Sheets("sort2").Cells(v, 1).Value = trip(1, v) 'calculate time
Sheets("sort2").Cells(v, 2).Value = trip(2, v) 'stop
Sheets("sort2").Cells(v, 3).Value = trip(3, v) 'stop_time_now
Sheets("sort2").Cells(v, 4).Value = trip(4, v) 'time
Sheets("sort2").Cells(v, 5).Value = trip(5, v) 'trip number
Sheets("sort2").Cells(v, 6).Value = v 'index 1
Next v
For u = 1 To UBound(trip, 1)
For v = 1 To UBound(trip, 2)
Sheets("alt_sort2").Cells(v, u).Value = trip(u, v)
Sheets("alt_sort2").Cells(v, u + 1).Value = u
Sheets("alt_sort2").Cells(v, u + 2).Value = v
Next v
Next u
End Sub
[/vba]