VB.NET中如何实现泛型排序算法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计488个文字,预计阅读时间需要2分钟。
vb.net模块_4_泛型演示Sub Main() Dim list As New List(Of Person) list.AddRange(New Person() {New Person(Ken, 36), New Person(Allen, 56), New Person(Mary, 28)}) Print2(list)End Sub
泛型排序 (VB.NET)
Module _4_Generic_Demo
??? Sub Main()
??????? Dim list As New List(Of Person)
??????? list.AddRange(New Person() {New Person("Ken", 36), New Person("Allen", 56), New Person("Mary", 28)})
??????? Print2(list)
??????? list.Sort()
??????? Print2(list)
??????? list.Sort(New NameComparer)
??????? Print2(list)
??? End Sub
??? Sub Print(ByVal list As List(Of Integer))
??????? For Each i As Integer In list
??????????? Console.WriteLine(i)
??????? Next
??????? Console.WriteLine("--------------------------------------------------")
??? End Sub
??? Sub Print2(ByVal list As List(Of Person))
??????? For Each i As Person In list
??????????? Console.WriteLine(i.Name & " : " & i.Age)
??????? Next
??????? Console.WriteLine("--------------------------------------------------")
??? End Sub
End Module
Class Person
??? ‘Implements IComparable
??? Implements IComparable(Of Person)
??? Public Name As String
??? Public Age As Integer
??? Sub New(ByVal n As String, ByVal a As Integer)
??????? Name = n
??????? Age = a
??? End Sub
??? ‘Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
??? ‘End Function
??? Public Function CompareTo(ByVal other As Person) As Integer Implements System.IComparable(Of Person).CompareTo
??????? Return Age - other.Age
??????? ‘Return Name.CompareTo(other.Name)
??? End Function
End Class
Class NameComparer
??? ‘Implements IComparer
??? Implements IComparer(Of Person)
??? ‘Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
??? ‘End Function
??? Public Function Compare(ByVal x As Person, ByVal y As Person) As Integer Implements System.Collections.Generic.IComparer(Of Person).Compare
??????? Return x.Name.CompareTo(y.Name)
??? End Function
End Class
如有错误 欢迎指正
原文:大专栏 泛型排序 (VB.NET)
本文共计488个文字,预计阅读时间需要2分钟。
vb.net模块_4_泛型演示Sub Main() Dim list As New List(Of Person) list.AddRange(New Person() {New Person(Ken, 36), New Person(Allen, 56), New Person(Mary, 28)}) Print2(list)End Sub
泛型排序 (VB.NET)
Module _4_Generic_Demo
??? Sub Main()
??????? Dim list As New List(Of Person)
??????? list.AddRange(New Person() {New Person("Ken", 36), New Person("Allen", 56), New Person("Mary", 28)})
??????? Print2(list)
??????? list.Sort()
??????? Print2(list)
??????? list.Sort(New NameComparer)
??????? Print2(list)
??? End Sub
??? Sub Print(ByVal list As List(Of Integer))
??????? For Each i As Integer In list
??????????? Console.WriteLine(i)
??????? Next
??????? Console.WriteLine("--------------------------------------------------")
??? End Sub
??? Sub Print2(ByVal list As List(Of Person))
??????? For Each i As Person In list
??????????? Console.WriteLine(i.Name & " : " & i.Age)
??????? Next
??????? Console.WriteLine("--------------------------------------------------")
??? End Sub
End Module
Class Person
??? ‘Implements IComparable
??? Implements IComparable(Of Person)
??? Public Name As String
??? Public Age As Integer
??? Sub New(ByVal n As String, ByVal a As Integer)
??????? Name = n
??????? Age = a
??? End Sub
??? ‘Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
??? ‘End Function
??? Public Function CompareTo(ByVal other As Person) As Integer Implements System.IComparable(Of Person).CompareTo
??????? Return Age - other.Age
??????? ‘Return Name.CompareTo(other.Name)
??? End Function
End Class
Class NameComparer
??? ‘Implements IComparer
??? Implements IComparer(Of Person)
??? ‘Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
??? ‘End Function
??? Public Function Compare(ByVal x As Person, ByVal y As Person) As Integer Implements System.Collections.Generic.IComparer(Of Person).Compare
??????? Return x.Name.CompareTo(y.Name)
??? End Function
End Class
如有错误 欢迎指正
原文:大专栏 泛型排序 (VB.NET)

