E
ExcelMonkey
I have a 2D array which I am attempting to sort. The print-out below shows
the data in its original form and then what it looks like after the sort. I
am trying to sort based on the 5th column. But as you can see, the sorted
range suffers from 2 errors. Firstly, the first item in column 5 is 22.
Secondly, the strings in the first column are in their original order.
Note that each unit is repeated twice in the array. Is the problem based on
the fact that I have repeated data? Or have I slipped a digit in my bubble
sort?
Unsorted Data
Unit 1 C E 241 22
Unit 2 D D 313 13
Unit 3 C B 207 31
Unit 1 C E 61 33
Unit 2 D D 87 5
Unit 3 C B 87 13
************************
Sorted Data
Unit 1 C E 241 22
Unit 2 D D 87 5
Unit 3 D D 313 13
Unit 1 C B 87 13
Unit 2 C B 207 31
Unit 3 C E 61 33
The array with the data is called "HourData". The code I use to sort the
array is as follows:
BubbleSort2D HourData, 4 '5th column = 4th column in 0 based array
For Counter = 1 To TotalUnitBids
Debug.Print HourData(Counter - 1, 0) & " " & HourData(Counter - 1,
1) & " " & HourData(Counter - 1, 2) & " " & HourData(Counter - 1,
3) & " " & HourData(Counter - 1, 4)
Next
********************************************************
Function BubbleSort2D(PassedArray As Variant, col As Long)
' Sorts an array using bubble sort algorithm in descending order using
'column as sort criteria
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer, k As Integer
Dim Temp As Variant
First = LBound(PassedArray, 1)
Last = UBound(PassedArray, 1)
For i = 1 To Last - 1
For j = i + 1 To Last
If PassedArray(i, col) > PassedArray(j, col) Then
For k = 1 To UBound(PassedArray, 2)
Temp = PassedArray(j, k)
PassedArray(j, k) = PassedArray(i, k)
PassedArray(i, k) = Temp
Next k
End If
Next j
Next i
End Function
Thanks
EM
the data in its original form and then what it looks like after the sort. I
am trying to sort based on the 5th column. But as you can see, the sorted
range suffers from 2 errors. Firstly, the first item in column 5 is 22.
Secondly, the strings in the first column are in their original order.
Note that each unit is repeated twice in the array. Is the problem based on
the fact that I have repeated data? Or have I slipped a digit in my bubble
sort?
Unsorted Data
Unit 1 C E 241 22
Unit 2 D D 313 13
Unit 3 C B 207 31
Unit 1 C E 61 33
Unit 2 D D 87 5
Unit 3 C B 87 13
************************
Sorted Data
Unit 1 C E 241 22
Unit 2 D D 87 5
Unit 3 D D 313 13
Unit 1 C B 87 13
Unit 2 C B 207 31
Unit 3 C E 61 33
The array with the data is called "HourData". The code I use to sort the
array is as follows:
BubbleSort2D HourData, 4 '5th column = 4th column in 0 based array
For Counter = 1 To TotalUnitBids
Debug.Print HourData(Counter - 1, 0) & " " & HourData(Counter - 1,
1) & " " & HourData(Counter - 1, 2) & " " & HourData(Counter - 1,
3) & " " & HourData(Counter - 1, 4)
Next
********************************************************
Function BubbleSort2D(PassedArray As Variant, col As Long)
' Sorts an array using bubble sort algorithm in descending order using
'column as sort criteria
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer, k As Integer
Dim Temp As Variant
First = LBound(PassedArray, 1)
Last = UBound(PassedArray, 1)
For i = 1 To Last - 1
For j = i + 1 To Last
If PassedArray(i, col) > PassedArray(j, col) Then
For k = 1 To UBound(PassedArray, 2)
Temp = PassedArray(j, k)
PassedArray(j, k) = PassedArray(i, k)
PassedArray(i, k) = Temp
Next k
End If
Next j
Next i
End Function
Thanks
EM