Hi all, I am using some code from Microsoft, so that I can sort a bindinglist.
For some reason, it is not sorting correctly. It ALMOST works...but for some reason, a small number of items in the list are not being sorted.
For testing purposes, I am sorting the list based on a integer property of a custom object. Each object has property, "RandomInteger" which is a random number between 1-10000.
Example: Object1.RandomInteger = 1923
Object2.RandomInteger = 3
Object3.RandomInteger = 9188
Object4.RandomInteger = 7
Object5.RandomInteger = 10
So after sorting the list of custom objects by their RandomInteger property, I should get them in this order...
Object1.RandomInteger = 3
Object2.RandomInteger = 7
Object3.RandomInteger = 10
Object4.RandomInteger = 1923
Object5.RandomInteger = 9188
However, I am getting them like this.
Object1.RandomInteger = 3
Object2.RandomInteger = 7
Object3.RandomInteger = 10
Object4.RandomInteger = 9188
Object5.RandomInteger = 1923
Keep in mind, many numbers are sorted correctly when using a list larger than could be displayed on these forums. It's around every 25th item that it is not sorted correctly. I have no clue why.
Here is the overriden ApplySortCore method from my SortableBindingList class..
Protected Overrides Sub ApplySortCore(ByVal prop As PropertyDescriptor, _
ByVal direction As ListSortDirection)
sortedList = New ArrayList
' Check to see if the property type we are sorting by implements
' the IComparable interface.
Dim interfaceType As Type = prop.PropertyType.GetInterface("IComparable")
If interfaceType IsNot Nothing Then
' If so, set the SortPropertyValue and SortDirectionValue.
sortPropertyValue = prop
sortDirectionValue = direction
unsortedItems = New ArrayList(Me.Count)
' Loop through each item, adding it the the sortedItems ArrayList.
For Each item As Object In Me.Items
sortedList.Add(prop.GetValue(item))
unsortedItems.Add(item)
Next
' Call Sort on the ArrayList.
sortedList.Sort()
Dim temp As T
' Check the sort direction and then copy the sorted items
' back into the list.
If (direction = ListSortDirection.Descending) Then
sortedList.Reverse()
End If
For i As Integer = 0 To Me.Count - 1
Dim position As Integer = Find(prop.Name, sortedList(i))
If position <> i Then
temp = Me(i)
Me(i) = Me(position)
Me(position) = temp
End If
Next
' Raise the ListChanged event so bound controls refresh their
' values.
OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
Else
' If the property type does not implement IComparable, let the user
' know.
Throw New NotSupportedException("Cannot sort by " & prop.Name & ". This" & _
prop.PropertyType.ToString() & " does not implement IComparable")
End If
End Sub
EDIT: P.S. According to the debugger, the items in SortedList are sorted correctly, however, when this For Loop is done executing and I check the values in Me(which is a list obviously) in the debugger, most values are sorted correctly, but not all...
For some reason, it is not sorting correctly. It ALMOST works...but for some reason, a small number of items in the list are not being sorted.
For testing purposes, I am sorting the list based on a integer property of a custom object. Each object has property, "RandomInteger" which is a random number between 1-10000.
Example: Object1.RandomInteger = 1923
Object2.RandomInteger = 3
Object3.RandomInteger = 9188
Object4.RandomInteger = 7
Object5.RandomInteger = 10
So after sorting the list of custom objects by their RandomInteger property, I should get them in this order...
Object1.RandomInteger = 3
Object2.RandomInteger = 7
Object3.RandomInteger = 10
Object4.RandomInteger = 1923
Object5.RandomInteger = 9188
However, I am getting them like this.
Object1.RandomInteger = 3
Object2.RandomInteger = 7
Object3.RandomInteger = 10
Object4.RandomInteger = 9188
Object5.RandomInteger = 1923
Keep in mind, many numbers are sorted correctly when using a list larger than could be displayed on these forums. It's around every 25th item that it is not sorted correctly. I have no clue why.
Here is the overriden ApplySortCore method from my SortableBindingList class..
Protected Overrides Sub ApplySortCore(ByVal prop As PropertyDescriptor, _
ByVal direction As ListSortDirection)
sortedList = New ArrayList
' Check to see if the property type we are sorting by implements
' the IComparable interface.
Dim interfaceType As Type = prop.PropertyType.GetInterface("IComparable")
If interfaceType IsNot Nothing Then
' If so, set the SortPropertyValue and SortDirectionValue.
sortPropertyValue = prop
sortDirectionValue = direction
unsortedItems = New ArrayList(Me.Count)
' Loop through each item, adding it the the sortedItems ArrayList.
For Each item As Object In Me.Items
sortedList.Add(prop.GetValue(item))
unsortedItems.Add(item)
Next
' Call Sort on the ArrayList.
sortedList.Sort()
Dim temp As T
' Check the sort direction and then copy the sorted items
' back into the list.
If (direction = ListSortDirection.Descending) Then
sortedList.Reverse()
End If
For i As Integer = 0 To Me.Count - 1
Dim position As Integer = Find(prop.Name, sortedList(i))
If position <> i Then
temp = Me(i)
Me(i) = Me(position)
Me(position) = temp
End If
Next
' Raise the ListChanged event so bound controls refresh their
' values.
OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
Else
' If the property type does not implement IComparable, let the user
' know.
Throw New NotSupportedException("Cannot sort by " & prop.Name & ". This" & _
prop.PropertyType.ToString() & " does not implement IComparable")
End If
End Sub
EDIT: P.S. According to the debugger, the items in SortedList are sorted correctly, however, when this For Loop is done executing and I check the values in Me(which is a list obviously) in the debugger, most values are sorted correctly, but not all...
Code:
For i As Integer = 0 To Me.Count - 1
Dim position As Integer = Find(prop.Name, sortedList(i))
If position <> i Then
temp = Me(i)
Me(i) = Me(position)
Me(position) = temp
End If
Next