Partial Class test_t_Compare
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c As String = Me.DropDownList1.SelectedValue
Dim b As Boolean = IIf(Me.DropDownList2.SelectedValue = "1", True, False)
Dim list As List(Of TestSortData) = New List(Of TestSortData)
list.Add(New TestSortData(1, 4, 3))
list.Add(New TestSortData(1, 2, 1))
list.Add(New TestSortData(1, 2, 3))
list.Add(New TestSortData(5, 2, 3))
list.Add(New TestSortData(3, 6, 3))
list.Add(New TestSortData(3, 1, 3))
list.Sort(New TestSortDataComparer(c, b))
For Each item As TestSortData In list
Response.Write(item.Clm1.ToString() + " - " + item.Clm2.ToString() + " - " + item.Clm3.ToString() + "<br>")
Next
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim list As ArrayList = New ArrayList()
list.Add("3")
list.Add("2")
list.Add("1")
list.Sort()
For Each item As String In list
Response.Write(item + " - ")
Next
End Sub
End Class
Public Class TestSortData
Private m_clm1 As Int32
Private m_clm2 As Int32
Private m_clm3 As Int32
Public Sub New(ByVal c1 As Int32, ByVal c2 As Int32, ByVal c3 As Int32)
m_clm1 = c1
m_clm2 = c2
m_clm3 = c3
End Sub
Public Property Clm1() As Int32
Get
Return m_clm1
End Get
Set(ByVal value As Int32)
m_clm1 = value
End Set
End Property
Public Property Clm2() As Int32
Get
Return m_clm2
End Get
Set(ByVal value As Int32)
m_clm2 = value
End Set
End Property
Public Property Clm3() As Int32
Get
Return m_clm3
End Get
Set(ByVal value As Int32)
m_clm3 = value
End Set
End Property
End Class
Public Class TestSortDataComparer
Implements IComparer(Of TestSortData)
Private m_SortField As String
Private m_IsAsc As Boolean
Public Sub New(ByVal sort_field As String, Optional ByVal is_asc As Boolean = True)
Me.m_SortField = sort_field
Me.m_IsAsc = is_asc
End Sub
Public Function Compare(ByVal x As TestSortData, ByVal y As TestSortData) As Integer Implements System.Collections.Generic.IComparer(Of TestSortData).Compare
Dim seq As Int32 = 1
If Not Me.m_IsAsc Then seq = -1
Select Case Me.m_SortField
Case "C1"
Dim rs21 As Int32 = x.Clm1.CompareTo(y.Clm1)
If rs21 = 0 Then
Dim rs22 As Int32 = x.Clm2.CompareTo(y.Clm2)
If rs22 = 0 Then
Dim rs23 As Int32 = x.Clm3.CompareTo(y.Clm3)
If rs23 = 0 Then
Return 0
Else
Return rs23
End If
Else
Return rs22
End If
Else
If rs21 > 0 Then rs21 = 1
If rs21 < 0 Then rs21 = -1
rs21 = rs21 * seq
Return rs21
End If
Case "C2"
..............
Case "C3"
...............
End Select
End Function
End Class