Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4,VB5,VB6
Expertise: Intermediate
Oct 13, 2001

Sorting on multiple keys

Frequently you need to sort arrays of records using multiple keys. This may be required since one single key does not uniquely identify a record (e.g. you may need both LastName and FirstName to select a given employee in a large company where people with same name work), or it may be necessary for reporting chores (for instance, you might sort the list of employees on their department first, and then on their name: the neat result is a list of employees grouped by their department and sorted alphabetically within each group).

If you need to sort on multiple keys you may follow different approaches. When all the keys that are involved are of string type, you may concatenate them to form a single key, then indirectly sort the array on that compound key. This approach works correctly only when working with fixed-length strings, otherwise it is up to you to make all string keys of same length. In the next example I'll show how an array of employees data can be sorted on the compound key "dept + name":


Type TEmployees
    name As String * 40
    dept As String * 12
    salary As Currency
End Type

' load the array from disk
ReDim employees(1 To 1) As TEmployees
Open "employees.dat" For Binary As #1
numEls = LOF(1) / Len(employees(1))
ReDim employees(1 To numEls) As TEmployees
Get #1, , employees()
Close #1

' create a temporary array holding key data
ReDim keys(1 To numEls) As String
For index = 1 To numEls
    keys(index) = employees(index).dept & employees(index).name
Next
' perform the indexed sort in descending order
ReDim ndx(1 To numEls) As Long
NdxShellSort keys(), ndx(), , True

' print the name of employees 
' in each department
dept = ""
For index = 1 To numEls
    With employees(ndx(index))
        If .dept <> dept Then
            dept = .dept
            Debug.Print "Dept: " & dept
        End If
        Debug.Print .name, .salary
    End With 
Next
Unfortunately, this simple method does not work when the compound key includes non-string (numerical) data. For instance, say we wish to sort on the key "dept + salary" the concatenation operation will not yield a useful string for our sorting purposes. However, we can still use this method by properly formatting the numerical data before building the compound key:

' create a temporary array holding key data
ReDim keys(1 To numEls) As String
For index = 1 To numEls
    keys(index) = employees(index).dept & Format$(employees(index).salary, _
        "0000000.00")
Next
The Format$ functions correctly align the numerical information so that it can be compared as it was a string. This methods works also with Integer and Long values, and even Single and Double data, but you must carefully choose the mask argument in the Format$ function so that no overflow error occurs.

Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date