Published: Tuesday, January 16, 2001
Sorting a Two-Dimensional Array using a Bubble Sort, Part 2
By Darren Neimke
Read Part 1
In Part 1 we examined how to search a two-dimensional
array of scalar variables. In this part, we'll examine how to sort a one-dimensional array of
objects!
Array Sorting With Objects
Now for our last trick, let's look at how you can sort an array
of objects. Again, we only need a minor modification to our sorting
routine so that we reference a property instead of a dimension of the
Array. The code for this modified function, ObjSorter, can be
seen below. (Also, feel free to try out the live demo.)
Sub ObjSorter( byRef arrArray )
Dim row, k
Dim objStartingKey, objSmallestKey
Dim ValueToCompare
Dim swap_pos
For row = 0 To UBound( arrArray ) - 1
'Start OUTER loop.
'Take a snapshot of the first element
'in the array because if there is a
'smaller value elsewhere in the array
'we'll need to do a swap.
Set objStartingKey = arrArray (row)
Set objSmallestKey = arrArray (row)
swap_pos = row
For k = row + 1 to UBound( arrArray )
' Start INNER loop.
' Call the objects Compare method of the current
' objSmallestKey object. Pass in the object that we are
' comparing against.
ValueToCompare = objSmallestKey.Compare( arrArray(k) )
' If 1 was returned then the value in arrArray(k)
' was smaller, so we do a swap.
If ValueToCompare = 1 Then
' This is now the lowest number -
' remember it's position.
swap_pos = k
Set objSmallestKey = arrArray ( k )
End If
Next
If swap_pos <> row Then
' If we get here then we are about to do a swap
' within the array.
Set arrArray ( swap_pos ) = objStartingKey
Set arrArray ( row ) = objSmallestKey
End If
Set objStartingKey = Nothing
Set objSmallestKey = Nothing
Next
End Sub
|
[
View the live demo]
Now, we'll create a template for our object and
create three instances of it.
Class Person
Public Name
Public Age
Public Sex
Public Function Compare (objOther)
' Default Compare for the Class is to sort by Name
Compare = strComp( me.Name, objOther.Name, vbTextCompare )
End Function
End Class
Set aPerson = New Person
aPerson.Name = "Paul"
aPerson.Age = 31
aPerson.Sex = "Male"
Set bPerson = New Person
bPerson.Name = "Darren"
bPerson.Age = 32
bPerson.Sex = "Male"
Set cPerson = New Person
cPerson.Name = "Anne"
cPerson.Age = 30
cPerson.Sex = "Female"
|
For more information on creating classes with VBScript, be sure to read:
Using Classes within VBScript!
Note that we have a public function in our class called Compare. By tweaking this function we
can determine how, exactly to sort each object instance. The Compare function above is set so that
the Person class instances are sorted by the Name property. Next, add each object to an array:
arrArray = Array(aPerson, bPerson, cPerson)
|
If we loop through our array and display the contents to the
screen:
For i = LBound(arrArray) to UBound(arrArray)
Response.Write arrArray(i).Name & " - "
Response.Write arrArray(i).Sex & " - "
Response.Write arrArray(i).Age & "<BR>"
Next
|
We get something that looks like this:
Paul - Male - 31
Darren - Male - 32
Anne - Female - 30
Now we simply call our routine, passing in our array and the
dimension that we wish to sort by:
call ObjSorter( arrArray )
|
After sorting the array of objects we can redisplay the array contents:
For i = LBound(arrArray) to UBound(arrArray)
Response.Write arrArray(i).Name & " - "
Response.Write arrArray(i).Sex & " - "
Response.Write arrArray(i).Age & "<BR>"
Next
|
And this time we get the sorted output by the Age property:
Anne - Female - 30
Paul - Male - 31
Darren - Male - 32
This wraps up this article on using Bubble Sort to sort two-dimensional arrays and arrays of
objects!
Happy Programming!
By Darren Neimke
Attachments:
View the 2-dimensional array sorting demo!
View the array of objects sorting demo!