Question Issues with sorting a Generic Collection

webdev_mu

New member
Joined
Jan 16, 2009
Messages
4
Programming Experience
5-10
We use a custom collection derived from CollectionBase to store our Business objects
VB.NET:
Public Class BECollection(Of t As AbstractEntity) Inherits CollectionBase
AbstractEntity is the class all our Business entity classes originate from. Things are looking good, except for some issues we are having when trying to sort the collection.
VB.NET:
	Public Sub Sort(ByVal propertyName As String, ByVal sortDirection As ListSortDirection)
		 InnerList.Sort(New Comparer(propertyName, sortDirection))
	End Sub

	Public Class Comparer
		Implements IComparer

		Dim _SortPropertyName As String
		Dim _SortDirection As ListSortDirection

		Public Sub New(ByVal sortPropertyName As String)
			_SortPropertyName = sortPropertyName
			_SortDirection = ListSortDirection.Ascending
		End Sub

		Public Sub New(ByVal sortPropertyName As String, _
		 ByVal sortDirection As ListSortDirection)
			_SortPropertyName = sortPropertyName
			_SortDirection = sortDirection
		End Sub

		Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
			Dim pi As PropertyInfo
			pi = x.GetType().GetProperty(_SortPropertyName)

			Dim x1 As IComparable
			Dim y1 As IComparable

			x1 = CType(pi.GetValue(x, Nothing), IComparable)
			y1 = CType(pi.GetValue(y, Nothing), IComparable)

			If _SortDirection = ListSortDirection.Ascending Then
				Return x1.CompareTo(y1)
			Else
				Return y1.CompareTo(x1)
			End If
		End Function

		Public Function Compare1(ByVal x As Object, _
			ByVal y As Object) As Integer Implements _
		 System.Collections.IComparer.Compare
		End Function

	End Class

To test the sort, I've created a simple Person class, that has two field, Name (a string), and ID (integer).

My test program is as follows:
VB.NET:
Sub Main()
		Dim coll As New SBECollection(Of Person)

		coll.Add(New Person("James", 99))
		coll.Add(New Person("Aaron", 101))
		coll.Add(New Person("BBrad", 100))

		Console.WriteLine("Default")
		For Each p As Person In coll
			Console.WriteLine(p.Name & " " & p.ID)
		Next

		coll.Sort("Name", ComponentModel.ListSortDirection.Ascending)
		Console.WriteLine("ASC")
		For Each p As Person In coll
			Console.WriteLine(p.Name & " " & p.ID)
		Next

		coll.Sort("Name", ComponentModel.ListSortDirection.Descending)
		Console.WriteLine("DESC")
		For Each p As Person In coll
			Console.WriteLine(p.Name & " " & p.ID)
		Next

		coll.Sort("ID", ComponentModel.ListSortDirection.Ascending)
		Console.WriteLine("ASC ID")
		For Each p As Person In coll
			Console.WriteLine(p.Name & " " & p.ID)
		Next

		coll.Sort("ID", ComponentModel.ListSortDirection.Descending)
		Console.WriteLine("DESC ID")
		For Each p As Person In coll
			Console.WriteLine(p.Name & " " & p.ID)
		Next
End Sub

The output of which, however, looks like this:
Default
James 99
Aaron 101
BBrad 100
ASC
BBrad 100
Aaron 101
James 99
DESC
James 99
Aaron 101
BBrad 100
ASC ID
BBrad 100
Aaron 101
James 99
DESC ID
James 99
Aaron 101
BBrad 100
the sort isn't sorting all the items in the collection, it seems. There must be a flaw in the way I'm implementing IComparer, but I can't find what it is.

Any help would be appriciated.
 
Pardon my dumb question, but is your sort routine running at all?

Your Compare() function that actually seems to do some comparing, isnt the one that implements the interface (so it won't get called)...
...and the Compare1() function that implements the interface, doesnt actually do anything because there is no body code to the function (so it wont make the sorter sort)


How did you get a function that does nothing, to compile? Doesnt VB tell you that you forgot to return a value?
 
In further proof that there are no stupid questions, only stupid programmers, you are of course correct, and I totally missed it after staring at it for the better part of a day (want to buy a second pair of eyes!)

Interestingly enough, I did manager to find where the original developer managed to find this code (or rather, an earlier iteration of it, that's commented out in the current production release)

Visual Studio Magazine Implement Custom Generic Collections Listing 3

Thanks!
 
Ahh.. C#... Such a beautiful language.. None of this "method named XYZ actually implements interface method ABC" malarkey ;)
 
Back
Top