**SORTED :) tx rj** Sorting 2D arrays on any column
Gang,
I've just spent ages searching the site. There's a huge amount of stuff on sorting arrays, with attendant debate about which algo is best.
But as far as I can see, there's none that do a sort of a 2d array on any column. I'd like to see the user quizzed for "What field (=column) do you want to sort the records (=rows) on?" and Robert's your Dad's* brother.
Anyone?
* or Mum's
Last edited by Jim Brown; Aug 8th, 2002 at 05:22 AM.
Basically it comes down to how you feed the sort algorithm...
so if you had
rec1: LastName FirstName MiddleName
you feed the LastName field into the Sort algorithm, and modifiy the algorithm to compare FirstName's only if the LastName comparison is indetermine (the names are equal). As you see below, if the first bolded line comparison cases to 0 (equal), the algorithm then checks the FirstNames (2nd bolded line), if those are equal, check the middlenames...
VB Code:
Do While incr > 0
For i = incr To UBound(patientarray) Step 1
j = i - incr
Do While j >= 0
[b]Select Case StrComp(patientarray(j).LName, patientarray(j + incr).LName, 1)[/b]
Case 1:
temp = patientarray(j)
patientarray(j) = patientarray(j + incr)
patientarray(j + incr) = temp
j = j - incr
Case 0:
[b]Select Case StrComp(patientarray(j).FName, patientarray(j + incr).FName, 1)[/b]
Case 1:
temp = patientarray(j)
patientarray(j) = patientarray(j + incr)
patientarray(j + incr) = temp
j = j - incr
Case 0:[b]
Select Case StrComp(patientarray(j).MName, patientarray(j + incr).MName, 1)[/b]
Case 1:
temp = patientarray(j)
patientarray(j) = patientarray(j + incr)
patientarray(j + incr) = temp
j = j - incr
Case 0:
Select Case StrComp(patientarray(j).BDate, patientarray(j + incr).BDate, 0)
Case 1:
temp = patientarray(j)
patientarray(j) = patientarray(j + incr)
patientarray(j + incr) = temp
j = j - incr
Case 0:
Or, to sort on just any ONE field (meaning not considering the order of other fields), you simply feed that field to the algorithm...
For this code, add two textboxes (txtArrayOutput) (Index 0 for original array, index 1 for sorted array), you need 2 buttons, and a text box, labelled as the code suggests:
Private Sub txtColumn_KeyPress(KeyAscii As Integer)
'Disallow non-numeric entries
If ((KeyAscii < vbKey0) Or (KeyAscii > vbKey9)) Then
If (KeyAscii <> vbKeyBack) Then KeyAscii = 0
End If
End Sub
I know it looks long, but there's auxilliary crap in there (creating an array, displaying the results, etc). Also, I've implemented this on a variant array, using a bubble sort method I pinched from Jamie. The sort method is arbitrary really, this one just didn't involve much code.
I'd have gone further and sorted on multiple columns, but the logic behind that escapes me at the moment. Anyway, the architecture is there, you're a smart boy, you can work it out.
To implement this fully in your solution, I suggest creating a 2nd 2D array to store a pair of COLUMN_NAME, COLUMN_INDEX for a better UI. Hopefully you can slip all that in there.