When you think ASP, think...
 Recent Articles All Articles ASP.NET Articles ASPFAQs.com Related Web Technologies User Tips! Coding Tips

 Sections: Sample Chapters JavaScript Tutorials MSDN Communities Hub Official Docs Security Stump the SQL Guru! XML Info Information: Feedback Author an Article
Published: Tuesday, January 16, 2001

# Sorting a Two-Dimensional Array using a Bubble Sort, Part 2

By Darren Neimke

• 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!

- continued -

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" ```

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 & "
" 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 & "
" 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!

•  ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article

 #brand-footer{text-align:center;margin:0 auto;} #commonfooterpropertytext{padding-bottom: 12px;} #commonfootercopyright{padding: 3px;} #eweekBrand-footer{text-align:center;margin:0 auto} #footerDisclaimerText{margin: 15px 10px 0px 10px; text-align: justify;} Advertiser Disclosure: Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.