In the previous tutorial on Visual Basic Collections, we looked at various tools we have at our disposal for listing all my Visual Basic books. We chose the Visual Basic Collection Class and looked at the syntax for creating, adding items to and iterating the contents of these classes. In this tutorial, we will build on that and look at a technique to sort a Visual Basic collection class.
Open your Collection class project and add this new AddItem routine.
' Add an item at its correct position. Private Sub AddItem(ByRef collectionBooks As Collection, ByVal item As String) Dim i As Integer ' See where the item belongs. For i = 1 To collectionBooks.Count If collectionBooks.Item(i) >= item Then Exit For End If Next i ' Insert the item. If i > collectionBooks.Count Then ' Add at the end. collectionBooks.Add(item) Else ' Add at the right position. collectionBooks.Add(item, , i) End If End Sub
(This is courtesy of http://www.vb-helper.com/howto_sorted_collection.html which is a great resource for more advanced VB topics)
Replace the Form Load code with
Dim collectionBooks As New Collection AddItem(collectionBooks, "Microsoft Visual Basic 2010 Step By Step") AddItem(collectionBooks, "Sams Teach Yourself Visual Basic 2012 in 24 Hours, Complete Starter Kit") AddItem(collectionBooks, "Distributed Applications with Microsoft Visual Basic 6.0 McSd Training Kit : For Exam 70-175") AddItem(collectionBooks, "Microsoft® ASP.NET Programming with Microsoft Visual Basic® .NET Version 2003 Step By Step") AddItem(collectionBooks, "Visual Basic 6 Design Patterns") AddItem(collectionBooks, "Excel VBA Programming For Dummies") AddItem(collectionBooks, "Learn to Program with Visual Basic") AddItem(collectionBooks, "Visual Basic 6 Complete") AddBooksToListBox(collectionBooks)
The title suggests we will be sorting the collection which is perhaps a little misleading. Instead we have ensured that the items are added in the correct place when we add the item by adding it in the right place by using the algorithm I plagarised from <a href=”http://www.vb-helper.com/howto_sorted_collection.html”>http://www.vb-helper.com/howto_sorted_collection.html</a>. This is an important lesson, don’t re-invent the wheel! I wish we had the internet around when I started programming. The solutions are out there, you just need to know where to find them. If somebody else has gone to the trouble of sorting a Visual Basic Collection Class, why would I even consider writing one myself? I had to understand the routine and how I could apply it, but I didn’t need to work it out again.
Lets look closer at the routine. The syntax we have used to add an item to a Collection has been
If you’d played around with Intellisense you’d have noticed that the Add method had other optional arguments.
- Key – a string value where you can specify your own key for the item
- Before – You can pass the Key or the Index of the item to insert your new item before. The Index is the position of the item in the collection – the first being 1 and the last being collection.Count.
- After – You can pass the Key or the Index of the item to insert your new item after.
In our AddItem routine we’d used the syntax
' Insert the item. If i > bookCollection.Count Then ' Add at the end. bookCollection.Add(item) Else ' Add at the right position. bookCollection.Add(item, , i) End If</pre>
bookCollection.Add(item, , i)
inserted the new item before the position i. The syntax
bookCollection.Add(item, , , i)
Would have inserted the new item after the position specified by i. Which would have been wrong in this case but it was worth pointing out anyway.
Run the application and you should see the ListBox with all my Visual Basic books, only this time sorted in alphabetical order.
Next up, let’s enhance the application and search the contents of this collection.