本文整理匯總了VB.NET中System.Collections.Generic.Comparer<T>類的典型用法代碼示例。如果您正苦於以下問題:VB.NET Comparer<T>類的具體用法?VB.NET Comparer<T>怎麽用?VB.NET Comparer<T>使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Comparer<T>類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Program
' 導入命名空間
Imports System.Collections.Generic
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim Boxes As New List(Of Box)()
Boxes.Add(New Box(4, 20, 14))
Boxes.Add(New Box(12, 12, 12))
Boxes.Add(New Box(8, 20, 10))
Boxes.Add(New Box(6, 10, 2))
Boxes.Add(New Box(2, 8, 4))
Boxes.Add(New Box(2, 6, 8))
Boxes.Add(New Box(4, 12, 20))
Boxes.Add(New Box(18, 10, 4))
Boxes.Add(New Box(24, 4, 18))
Boxes.Add(New Box(10, 4, 16))
Boxes.Add(New Box(10, 2, 10))
Boxes.Add(New Box(6, 18, 2))
Boxes.Add(New Box(8, 12, 4))
Boxes.Add(New Box(12, 10, 8))
Boxes.Add(New Box(14, 6, 6))
Boxes.Add(New Box(16, 6, 16))
Boxes.Add(New Box(2, 8, 12))
Boxes.Add(New Box(4, 24, 8))
Boxes.Add(New Box(8, 6, 20))
Boxes.Add(New Box(18, 18, 12))
' Sort by an Comparer<T> implementation that sorts
' first by the length.
Boxes.Sort(New BoxLengthFirst())
Console.WriteLine("H - L - W")
Console.WriteLine("==========")
For Each bx As Box In Boxes
Console.WriteLine("{0}" & vbTab & "{1}" & vbTab & "{2}", _
bx.Height.ToString(), bx.Length.ToString(), _
bx.Width.ToString())
Next bx
Console.WriteLine()
Console.WriteLine("H - L - W")
Console.WriteLine("==========")
' Get the default comparer that
' sorts first by the height.
Dim defComp As Comparer(Of Box) = Comparer(Of Box).Default
' Calling Boxes.Sort() with no parameter
' is the same as calling Boxs.Sort(defComp)
' because they are both using the default comparer.
Boxes.Sort()
For Each bx As Box In Boxes
Console.WriteLine("{0}" & vbTab & "{1}" & vbTab & "{2}", _
bx.Height.ToString(), _
bx.Length.ToString(), _
bx.Width.ToString())
Next bx
' This explicit interface implementation
' compares first by the length.
' Returns -1 because the length of BoxA
' is less than the length of BoxB.
Dim LengthFirst As New BoxLengthFirst()
Dim bc As Comparer(Of Box) = CType(LengthFirst, Comparer(Of Box))
Dim BoxA As New Box(2, 6, 8)
Dim BoxB As New Box(10, 12, 14)
Dim x As Integer = LengthFirst.Compare(BoxA, BoxB)
Console.WriteLine()
Console.WriteLine(x.ToString())
End Sub
End Class
Public Class BoxLengthFirst
Inherits Comparer(Of Box)
' Compares by Length, Height, and Width.
Public Overrides Function Compare(ByVal x As Box, ByVal y As Box) As Integer
If x.Length.CompareTo(y.Length) <> 0 Then
Return x.Length.CompareTo(y.Length)
ElseIf x.Height.CompareTo(y.Height) <> 0 Then
Return x.Height.CompareTo(y.Height)
ElseIf x.Width.CompareTo(y.Width) <> 0 Then
Return x.Width.CompareTo(y.Width)
Else
Return 0
End If
End Function
End Class
' This class is not demonstrated in the Main method
' and is provided only to show how to implement
' the interface. It is recommended to derive
' from Comparer<T> instead of implementing IComparer<T>.
Public Class BoxComp
Implements IComparer(Of Box)
' Compares by Height, Length, and Width.
Public Function Compare(ByVal x As Box, ByVal y As Box) As Integer Implements _
IComparer(Of Box).Compare
If x.Height.CompareTo(y.Height) <> 0 Then
Return x.Height.CompareTo(y.Height)
ElseIf x.Length.CompareTo(y.Length) <> 0 Then
Return x.Length.CompareTo(y.Length)
ElseIf x.Width.CompareTo(y.Width) <> 0 Then
Return x.Width.CompareTo(y.Width)
Else
Return 0
End If
End Function
End Class
Public Class Box
Implements IComparable(Of Box)
Public Sub New(ByVal h As Integer, ByVal l As Integer, ByVal w As Integer)
Me.Height = h
Me.Length = l
Me.Width = w
End Sub
Private privateHeight As Integer
Public Property Height() As Integer
Get
Return privateHeight
End Get
Private Set(ByVal value As Integer)
privateHeight = value
End Set
End Property
Private privateLength As Integer
Public Property Length() As Integer
Get
Return privateLength
End Get
Private Set(ByVal value As Integer)
privateLength = value
End Set
End Property
Private privateWidth As Integer
Public Property Width() As Integer
Get
Return privateWidth
End Get
Private Set(ByVal value As Integer)
privateWidth = value
End Set
End Property
Public Function CompareTo(ByVal other As Box) As Integer _
Implements IComparable(Of Box).CompareTo
' Compares Height, Length, and Width.
If Me.Height.CompareTo(other.Height) <> 0 Then
Return Me.Height.CompareTo(other.Height)
ElseIf Me.Length.CompareTo(other.Length) <> 0 Then
Return Me.Length.CompareTo(other.Length)
ElseIf Me.Width.CompareTo(other.Width) <> 0 Then
Return Me.Width.CompareTo(other.Width)
Else
Return 0
End If
End Function
End Class