
泛型排序 (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)
总结以上是内存溢出为你收集整理的泛型排序 (VB.NET)全部内容,希望文章能够帮你解决泛型排序 (VB.NET)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)