Files
OrganLit/clsGridSorter.vb
2026-03-07 19:38:21 -06:00

218 lines
8.3 KiB
VB.net

Imports System.Windows.Forms
Public Class clsGridSorter : Implements IComparer
Dim mintColumnIndexToSort As Integer
Dim mobjGridColumnSortRules() As clsGridColumnSortRule
Public Sub New()
mintColumnIndexToSort = 0
End Sub
Public Sub New(ByVal lintInputColumnToSort As Integer)
mintColumnIndexToSort = lintInputColumnToSort
End Sub
Public Sub New(ByVal lintInputColumnToSort As Integer, ByVal lobjGridColumnSortRules As clsGridColumnSortRule())
mintColumnIndexToSort = lintInputColumnToSort
mobjGridColumnSortRules = lobjGridColumnSortRules
End Sub
Public Property ColumnIndexToSort() As Integer
Get
Return mintColumnIndexToSort
End Get
Set(ByVal iValue As Integer)
mintColumnIndexToSort = iValue
End Set
End Property
Public Property ColumnSortRules() As clsGridColumnSortRule()
Get
Return mobjGridColumnSortRules
End Get
Set(ByVal iValue As clsGridColumnSortRule())
mobjGridColumnSortRules = iValue
End Set
End Property
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim row1 As ListViewItem
Dim row2 As ListViewItem
Dim lintReturnValue As Integer
row1 = CType(x, ListViewItem)
row2 = CType(y, ListViewItem)
If mintColumnIndexToSort > row1.SubItems.Count Then
Return -1
End If
If mobjGridColumnSortRules Is Nothing _
OrElse mintColumnIndexToSort >= mobjGridColumnSortRules.Length Then
'Default to string comparison
Dim s1 As String = row1.SubItems(0).Text
Dim s2 As String = row2.SubItems(0).Text
Return String.Compare(s1, s2)
End If
Select Case mobjGridColumnSortRules(mintColumnIndexToSort).SortType
Case clsGridColumnSortRule.enumSortType.SortAsNumber
Dim ldblValue1 As Double = Val(row1.SubItems(mintColumnIndexToSort).Text)
Dim ldblValue2 As Double = Val(row2.SubItems(mintColumnIndexToSort).Text)
lintReturnValue = CompareDouble(ldblValue1, ldblValue2)
If lintReturnValue = 0 Then
lintReturnValue = CompareSubColumns(row1, row2, Me.mobjGridColumnSortRules(mintColumnIndexToSort).SubColumnSort)
End If
Return lintReturnValue
Case clsGridColumnSortRule.enumSortType.SortAsString
Dim lstrValue1 As String = row1.SubItems(mintColumnIndexToSort).Text
Dim lstrValue2 As String = row2.SubItems(mintColumnIndexToSort).Text
lintReturnValue = CompareString(lstrValue1, lstrValue2)
If lintReturnValue = 0 Then
lintReturnValue = CompareSubColumns(row1, row2, Me.mobjGridColumnSortRules(mintColumnIndexToSort).SubColumnSort)
End If
Return lintReturnValue
Case Else 'No Sort here - go to subcolumn sorts, if any
lintReturnValue = CompareSubColumns(row1, row2, Me.mobjGridColumnSortRules(mintColumnIndexToSort).SubColumnSort)
Return lintReturnValue
End Select
Return 0
End Function
Private Function CompareString(ByVal lstrString1 As String, ByVal lstrString2 As String) As Integer
Return String.Compare(lstrString1, lstrString2)
End Function
Private Function CompareDouble(ByVal ldblDouble1 As Double, ByVal ldblDouble2 As Double) As Integer
If ldblDouble1 < ldblDouble2 Then
Return -1
Else
If ldblDouble2 < ldblDouble1 Then
Return 1
Else
Return 0
End If
End If
End Function
Private Function CompareSubColumns(ByVal row1 As ListViewItem, ByVal row2 As ListViewItem, ByVal lobjSubColumnSort As clsGridSubColumnSortRule) As Integer
Dim lintReturnValue As Integer
If lobjSubColumnSort Is Nothing Then
Return 0
Else
If lobjSubColumnSort.ColumnIndex > row1.SubItems.Count Then
Return -1
End If
Select Case lobjSubColumnSort.SortType
Case clsGridSubColumnSortRule.enumSortType.SortAsNumber
Dim ldblValue1 As Double = Val(row1.SubItems(lobjSubColumnSort.ColumnIndex).Text)
Dim ldblValue2 As Double = Val(row2.SubItems(lobjSubColumnSort.ColumnIndex).Text)
lintReturnValue = CompareDouble(ldblValue1, ldblValue2)
If lintReturnValue = 0 Then
lintReturnValue = CompareSubColumns(row1, row2, lobjSubColumnSort.SubColumnSortRule)
End If
Return lintReturnValue
Case clsGridSubColumnSortRule.enumSortType.SortAsString
Dim lstrValue1 As String = row1.SubItems(lobjSubColumnSort.ColumnIndex).Text
Dim lstrValue2 As String = row2.SubItems(lobjSubColumnSort.ColumnIndex).Text
lintReturnValue = CompareString(lstrValue1, lstrValue2)
If lintReturnValue = 0 Then
lintReturnValue = CompareSubColumns(row1, row2, lobjSubColumnSort.SubColumnSortRule)
End If
Return lintReturnValue
Case Else 'No Sort here - go to subcolumn sorts, if any
lintReturnValue = CompareSubColumns(row1, row2, lobjSubColumnSort.SubColumnSortRule)
Return lintReturnValue
End Select
End If
End Function
End Class
Public Class clsGridColumnSortRule
Public Enum enumSortType
SortAsString
SortAsNumber
SortNone
End Enum
Private menumSortType As enumSortType
Private mobjSubColumnSort As clsGridSubColumnSortRule
Public Sub New()
menumSortType = enumSortType.SortAsString
End Sub
Public Sub New(ByVal lenumSortType As enumSortType)
menumSortType = lenumSortType
End Sub
Public Property SortType() As enumSortType
Get
Return menumSortType
End Get
Set(ByVal iValue As enumSortType)
menumSortType = iValue
End Set
End Property
Public Property SubColumnSort() As clsGridSubColumnSortRule
Get
Return mobjSubColumnSort
End Get
Set(ByVal iValue As clsGridSubColumnSortRule)
mobjSubColumnSort = iValue
End Set
End Property
End Class
Public Class clsGridSubColumnSortRule
Public Enum enumSortType
SortAsString
SortAsNumber
End Enum
Private menumSortType As enumSortType
Private mintColumnIndex As Integer
Private mobjSubColumnSortRule As clsGridSubColumnSortRule
Public Sub New(ByVal lintColumnIndex As Integer)
mintColumnIndex = lintColumnIndex
menumSortType = enumSortType.SortAsString
End Sub
Public Sub New(ByVal lintColumnIndex As Integer, ByVal lobjSubColumnSortRule As clsGridSubColumnSortRule)
mintColumnIndex = lintColumnIndex
mobjSubColumnSortRule = lobjSubColumnSortRule
menumSortType = enumSortType.SortAsString
End Sub
Public Sub New(ByVal lintColumnIndex As Integer, ByVal lenumSortType As enumSortType, ByVal lobjSubColumnSortRule As clsGridSubColumnSortRule)
mintColumnIndex = lintColumnIndex
mobjSubColumnSortRule = lobjSubColumnSortRule
menumSortType = lenumSortType
End Sub
Public Property ColumnIndex() As Integer
Get
Return mintColumnIndex
End Get
Set(ByVal iValue As Integer)
mintColumnIndex = iValue
End Set
End Property
Public Property SortType() As enumSortType
Get
Return menumSortType
End Get
Set(ByVal iValue As enumSortType)
menumSortType = iValue
End Set
End Property
Public Property SubColumnSortRule() As clsGridSubColumnSortRule
Get
Return mobjSubColumnSortRule
End Get
Set(ByVal iValue As clsGridSubColumnSortRule)
mobjSubColumnSortRule = iValue
End Set
End Property
End Class