|
Imports rw.Data
Imports System.ComponentModel
Namespace Objects
''' <summary>
''' Comparer class for the ScheduleGeneral control.
''' </summary>
Class ScheduleGeneralComparer
Implements IComparer(Of ScheduleItemData)
Public Sub New()
End Sub
Public Function Compare(ByVal x As ScheduleItemData, ByVal y As ScheduleItemData) As Integer _
Implements IComparer(Of ScheduleItemData).Compare
Dim xTitleValue As IComparable = TryCast(x.TitleValue, IComparable)
If (xTitleValue Is Nothing) Then Return 0 ' when titles are not comparable, they're considered all equal
Dim yTitleValue As IComparable = TryCast(y.TitleValue, IComparable)
If (yTitleValue Is Nothing) Then Return 0
If (Not xTitleValue.Equals(yTitleValue)) Then
Return xTitleValue.CompareTo(yTitleValue)
End If
Dim xStartValue As IComparable = TryCast(x.StartValue, IComparable)
Dim yStartValue As IComparable = TryCast(y.StartValue, IComparable)
If (ShouldCompareNulls(xStartValue, yStartValue)) Then
Return CompareNulls(xStartValue, yStartValue)
End If
If (Not xStartValue.Equals(yStartValue)) Then
Return xStartValue.CompareTo(yStartValue)
End If
If (ShouldCompareNulls(x.EndValue, y.EndValue)) Then
Return CompareNulls(x.EndValue, y.EndValue)
End If
Return DirectCast(x.EndValue, IComparable).CompareTo(DirectCast(y.EndValue, IComparable))
End Function
Private Function CompareNulls(ByVal x As Object, ByVal y As Object) As Integer
If (x Is Nothing AndAlso y IsNot Nothing) Then Return -1
If (x IsNot Nothing AndAlso y Is Nothing) Then Return 1
If (x Is Nothing AndAlso y Is Nothing) Then Return 0
If (TypeOf x Is DBNull) And Not (TypeOf y Is DBNull) Then Return -1
If Not (TypeOf x Is DBNull) And (TypeOf y Is DBNull) Then Return 1
If (TypeOf x Is DBNull) And (TypeOf y Is DBNull) Then Return 0
End Function
Private Function ShouldCompareNulls(ByVal x As Object, ByVal y As Object) As Boolean
If x Is Nothing Then Return True
If y Is Nothing Then Return True
If (TypeOf x Is DBNull) Then Return True
If (TypeOf y Is DBNull) Then Return True
Return False
End Function
End Class
End Namespace
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.