G
Guest
I'm trying to figure out why I'm getting the following error message when
trying to radomly sort a list of custom objects. In particular I've noticed
the following strange behaviour I'm hoping someone could shed some light
on....
1. When the list contains only 3 objects the sort works fine, unless I
remove the 'if x.equals(y)' check in the compare function.
2. When there are more than 3 objects I get the error even when this check
is in place (albeit it usually takes a bit longer).
------------------------ERROR MESSAGE----------------------------
IComparer (or the IComparable methods it relies upon) did not return zero
when Array.Sort called x. CompareTo(x)
-------------------------CODE LISTING--------------------------------
Module Module1
Sub Main()
Dim People As New System.Collections.Generic.List(Of Person)
Dim Counter As Integer = 0
People.Add(New Person(1, "Brian"))
People.Add(New Person(2, "Darragh"))
People.Add(New Person(3, "Aoife"))
People.Add(New Person(4, "Aidan")) ' when only 3 people I don't seem
to have a problem unless I remove the if x.Equals(y) check below!
While True
Counter += 1
Console.WriteLine("Comparison {0}", Counter)
People.Sort(New Person.Random())
End While
End Sub
End Module
Public Class Person
Public ID As Integer
Public Name As String
Public Sub New(ByVal ID As Integer, ByVal Name As String)
Me.ID = ID
Me.Name = Name
End Sub
Public Overrides Function ToString() As String
Return String.Format("ID={0}, Name={1}", Me.ID, Me.Name)
End Function
Class Random
Implements System.Collections.Generic.IComparer(Of Person)
Private Shared r As New System.Random
Public Function Compare(ByVal x As Person, ByVal y As Person) As
Integer Implements System.Collections.Generic.IComparer(Of Person).Compare
Console.Write("Comparing: x=[{0}], y=[{1}]", x, y)
If x.Equals(y) Then Compare = 0 Else Compare = r.Next(-1, 2)
'removing this gives error when only 3 items
'Compare = r.Next(-1, 2)
Console.WriteLine("{0}Returning: {1}", vbTab, Compare)
End Function
End Class
End Class
trying to radomly sort a list of custom objects. In particular I've noticed
the following strange behaviour I'm hoping someone could shed some light
on....
1. When the list contains only 3 objects the sort works fine, unless I
remove the 'if x.equals(y)' check in the compare function.
2. When there are more than 3 objects I get the error even when this check
is in place (albeit it usually takes a bit longer).
------------------------ERROR MESSAGE----------------------------
IComparer (or the IComparable methods it relies upon) did not return zero
when Array.Sort called x. CompareTo(x)
-------------------------CODE LISTING--------------------------------
Module Module1
Sub Main()
Dim People As New System.Collections.Generic.List(Of Person)
Dim Counter As Integer = 0
People.Add(New Person(1, "Brian"))
People.Add(New Person(2, "Darragh"))
People.Add(New Person(3, "Aoife"))
People.Add(New Person(4, "Aidan")) ' when only 3 people I don't seem
to have a problem unless I remove the if x.Equals(y) check below!
While True
Counter += 1
Console.WriteLine("Comparison {0}", Counter)
People.Sort(New Person.Random())
End While
End Sub
End Module
Public Class Person
Public ID As Integer
Public Name As String
Public Sub New(ByVal ID As Integer, ByVal Name As String)
Me.ID = ID
Me.Name = Name
End Sub
Public Overrides Function ToString() As String
Return String.Format("ID={0}, Name={1}", Me.ID, Me.Name)
End Function
Class Random
Implements System.Collections.Generic.IComparer(Of Person)
Private Shared r As New System.Random
Public Function Compare(ByVal x As Person, ByVal y As Person) As
Integer Implements System.Collections.Generic.IComparer(Of Person).Compare
Console.Write("Comparing: x=[{0}], y=[{1}]", x, y)
If x.Equals(y) Then Compare = 0 Else Compare = r.Next(-1, 2)
'removing this gives error when only 3 items
'Compare = r.Next(-1, 2)
Console.WriteLine("{0}Returning: {1}", vbTab, Compare)
End Function
End Class
End Class