Avatar of Bob Schneider
Bob Schneider
Flag for United States of America asked on

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.

ASP

Avatar of undefined
Last Comment
ste5an

8/22/2022 - Mon
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.
ste5an

That's the reason why a comaprision function taking two array rows as parameters is used instead of a simple key comparision.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Bob Schneider

ASKER
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

Open in new window

SOLUTION
Lee W, MVP

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.