本文整理匯總了VB.NET中System.Comparison<T>代理的典型用法代碼示例。如果您正苦於以下問題:VB.NET Comparison<T>代理的具體用法?VB.NET Comparison<T>怎麽用?VB.NET Comparison<T>使用的例子?那麽, 這裏精選的代理代碼示例或許可以為您提供幫助。
在下文中一共展示了Comparison<T>代理的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Collections.Generic
Public Class Example
Private Shared Function CompareDinosByLength( _
ByVal x As String, ByVal y As String) As Integer
If x Is Nothing Then
If y Is Nothing Then
' If x is Nothing and y is Nothing, they're
' equal.
Return 0
Else
' If x is Nothing and y is not Nothing, y
' is greater.
Return -1
End If
Else
' If x is not Nothing...
'
If y Is Nothing Then
' ...and y is Nothing, x is greater.
Return 1
Else
' ...and y is not Nothing, compare the
' lengths of the two strings.
'
Dim retval As Integer = _
x.Length.CompareTo(y.Length)
If retval <> 0 Then
' If the strings are not of equal length,
' the longer string is greater.
'
Return retval
Else
' If the strings are of equal length,
' sort them with ordinary string comparison.
'
Return x.CompareTo(y)
End If
End If
End If
End Function
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)
dinosaurs.Add("Pachycephalosaurus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("")
dinosaurs.Add(Nothing)
dinosaurs.Add("Mamenchisaurus")
dinosaurs.Add("Deinonychus")
Display(dinosaurs)
Console.WriteLine(vbLf & "Sort with generic Comparison(Of String) delegate:")
dinosaurs.Sort(AddressOf CompareDinosByLength)
Display(dinosaurs)
End Sub
Private Shared Sub Display(ByVal lis As List(Of String))
Console.WriteLine()
For Each s As String In lis
If s Is Nothing Then
Console.WriteLine("(Nothing)")
Else
Console.WriteLine("""{0}""", s)
End If
Next
End Sub
End Class
輸出:
"Pachycephalosaurus" "Amargasaurus" "" (Nothing) "Mamenchisaurus" "Deinonychus" Sort with generic Comparison(Of String) delegate: (Nothing) "" "Deinonychus" "Amargasaurus" "Mamenchisaurus" "Pachycephalosaurus"
示例2: New
Public Class CityInfo
Dim cityName As String
Dim countryName As String
Dim pop2010 As Integer
Public Sub New(name As String, country As String, pop2010 As Integer)
Me.cityName = name
Me.countryName = country
Me.pop2010 = pop2010
End Sub
Public ReadOnly Property City As String
Get
Return Me.cityName
End Get
End Property
Public ReadOnly Property Country As String
Get
Return Me.countryName
End Get
End Property
Public ReadOnly Property Population As Integer
Get
Return Me.pop2010
End Get
End Property
Public Shared Function CompareByName(city1 As CityInfo, city2 As CityInfo) As Integer
Return String.Compare(city1.City, city2.City)
End Function
Public Shared Function CompareByPopulation(city1 As CityInfo, city2 As CityInfo) As Integer
Return city1.Population.CompareTo(city2.Population)
End Function
Public Shared Function CompareByNames(city1 As CityInfo, city2 As CityInfo) As Integer
Return String.Compare(city1.Country + city1.City, city2.Country + city2.City)
End Function
End Class
Module Example
Public Sub Main()
Dim NYC As New CityInfo("New York City", "United States of America", 8175133)
Dim Det As New CityInfo("Detroit", "United States of America", 713777)
Dim Paris As New CityInfo("Paris", "France", 2193031)
Dim cities As CityInfo() = { NYC, Det, Paris }
' Display ordered array.
DisplayArray(cities)
' Sort array by city name.
Array.Sort(cities, AddressOf CityInfo.CompareByName)
DisplayArray(cities)
' Sort array by population.
Array.Sort(cities, AddressOf CityInfo.CompareByPopulation)
DisplayArray(cities)
' Sort array by country + city name.
Array.Sort(cities, AddressOf CityInfo.CompareByNames)
DisplayArray(cities)
End Sub
Private Sub DisplayArray(cities() As CityInfo)
Console.WriteLine("{0,-20} {1,-25} {2,10}", "City", "Country/Region", "Population")
For Each city In cities
Console.WriteLine("{0,-20} {1,-25} {2,10:N0}", city.City, city.Country, city.Population)
Next
Console.WriteLine()
End Sub
End Module
輸出:
City Country/Region Population New York City United States of America 8,175,133 Detroit United States of America 713,777 Paris France 2,193,031 City Country/Region Population Detroit United States of America 713,777 New York City United States of America 8,175,133 Paris France 2,193,031 City Country/Region Population Detroit United States of America 713,777 Paris France 2,193,031 New York City United States of America 8,175,133 City Country/Region Population Paris France 2,193,031 Detroit United States of America 713,777 New York City United States of America 8,175,133