|
Post by Kelly Ethridge on Dec 27, 2005 16:03:39 GMT -5
So there I was adding new functionality to VBCorLib. I had included the Encoding.GetEncodings method which returns an array of EncodingInfo objects. I displayed the array in the console and decided I wanted them displayed sorted by their code page. I was doing this in the Main sub of a module and was thinking about how to sort them using cArray.Sort.
Since cArray.Sort doesn't know how to sort my objects, I was going to have to supply a custom comparer somehow. I thought I was going to have to add a class and implement the IComparer interface for custom comparisons.
I didn't want to have to add a class just to do a quick sort! I then remembered that the cArray.Sort method's Comparer argument could take a procedure address and use call backs. This is perfect for keeping everything in a module!
I wrote the very quick custom comparison routine right below the Main method:
Private Function SortCallback(ByRef x As EncodingInfo, ByRef y As EncodingInfo) As Long SortCallback = x.CodePage - y.CodePage End Function and my call to the sort routine became:
cArray.Sort Infos, AddressOf SortCallback And that was it. You couldn't ASK for an easier way to sort custom objects.
I keep forgetting all of the things VBCorLib can do, and keep getting reminded that there is usually an easy way to accomplish my tasks.
-Kelly
|
|