We use a custom collection derived from CollectionBase to store our Business objects
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.
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:
The output of which, however, looks like this:
Any help would be appriciated.
VB.NET:
Public Class BECollection(Of t As AbstractEntity) Inherits CollectionBase
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:
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.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
Any help would be appriciated.