Sorted Collections

A list box is just a collection with a different syntax. If you have a sorted list box class, you’re not very far from having a sorted collection class. So have at it. Clone XListBoxPlus. Delete the properties and methods unique to list boxes (those dealing with selected items and the list box interface). Change some names here and there, and you’ve got it.

Whoa! Not so fast. Collections can be either indexed or unindexed. Collections contain objects, not strings. You’ll need to deal with those differences. You could create a CSortedStrings class by cloning because a collection of strings is unindexed by definition. But a collection of objects sorted by a specified field is a different matter.

I’ve supplied you with code to sort collections in a procedural style. Check out SortCollection and BSearchCollection in SORT.BAS, and then look at how they are used to sort a collection of strings in TSORT.VBG. This should help you get started writing your own CSortedCollection class. But keep in mind that making your CSortedCollection class a wrapper for the Collection class probably will not be nearly as fast as writing the class from scratch. See the Challenge on page 179 for more opinions on this topic.