# Sort a Classic ASP array by two fields

I have a classic asp array with 6 fields.  I want to sort first on a string field (field 5) smallest to largest, and then on a numeric field (field 3) smallest to largest.  What is the best way to do that.  Typically these arrays have less than 200 rows.

Thank you.

ste5an

Use a standard quick sort. And replace the simple comparision with comparsion function implementing your sort logic.
Dr. Klahn

When sorting on multiple keys (viz., sorting the array and then resorting), quicksort can only be used for the first sort.  This is because quicksort won't preserve order in the secondary keys.  E.g.:

A1, A2, A3, B7, D12, H4, Z7

would quicksort to this order if sorted on the numeric secondary field:  A1, A2, A5, H6, B7, Z7, D12

But if the array is again quicksorted, this time on the alphabetic primary field, one might get:  A5, A1, A2, B7, D12, H6, Z7 which destroys the sort order in the secondary field (should have been A1, A2, A5).

So if sort order in secondary fields must be preserved, quicksort for the secondary field and bubble sort (or any other sort which preserves order) for the primary field.  Bubble sort preserves order in the secondary key.
That's the reason why a comaprision function taking two array rows as parameters is used instead of a simple key comparision.

Here is what I am doing now.  The first sort works to sort by time, but when I then want to sort that result into two groups by technique, the second sort does not preserve the first.

``````    'sort by time
For x = 0 To UBound(Finishers, 2) - 2
For y = x + 1 To UBound(Finishers, 2) - 1
If CSng(Finishers(3, x)) > CSng(Finishers(3, y)) Then
For z = 0 To 5
SortArr(z) = Finishers(z, x)
Finishers(z, x) = Finishers(z, y)
Finishers(z, y) = SortArr(z)
Next
End If
Next
Next

If sTechSort = "technique" Then
For x = 0 To UBound(Finishers, 2) - 2
For y = x + 1 To UBound(Finishers, 2) - 1
If Finishers(5, x) > Finishers(5, y) Then
For z = 0 To 5
SortArr(z) = Finishers(z, x)
Finishers(z, x) = Finishers(z, y)
Finishers(z, y) = SortArr(z)
Next
End If
Next
Next
End If``````
SOLUTION
Lee W, MVP

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.