当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET IComparer接口代码示例

本文整理汇总了VB.NET中System.Collections.IComparer接口的典型用法代码示例。如果您正苦于以下问题:VB.NET IComparer接口的具体用法?VB.NET IComparer怎么用?VB.NET IComparer使用的例子?那么, 这里精选的接口代码示例或许可以为您提供帮助。


在下文中一共展示了IComparer接口的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Example

' 导入命名空间
Imports System.Collections

Public Class Example
   Public Class ReverserClass : Implements IComparer
      ' Call CaseInsensitiveComparer.Compare with the parameters reversed.
      Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
             Implements IComparer.Compare
         Return New CaseInsensitiveComparer().Compare(y, x)
      End Function 
   End Class

   Public Shared Sub Main()
      ' Initialize a string array.
      Dim words() As String = { "The", "quick", "brown", "fox", "jumps", "over",
                         "the", "lazy", "dog" }

      ' Display the array values.
      Console.WriteLine("The array initially contains the following values:")
      PrintIndexAndValues(words)

      ' Sort the array values of the ArrayList using the default comparer.
      Array.Sort(words)
      Console.WriteLine("After sorting with the default comparer:")
      PrintIndexAndValues(words)

      ' Sort the array values using the reverse case-insensitive comparer.
      Array.Sort(words, new ReverserClass())
      Console.WriteLine("After sorting with the reverse case-insensitive comparer:")
      PrintIndexAndValues(words)
   End Sub 

   Public Shared Sub PrintIndexAndValues(list As IEnumerable)
      Dim i As Integer = 0
      For Each item In  list
         Console.WriteLine($"   [{i}]:  {item}")
         i += 1
      Next
      Console.WriteLine()
   End Sub 
End Class
开发者ID:VB.NET开发者,项目名称:System.Collections,代码行数:41,代码来源:IComparer

输出:

The array initially contains the following values:
[0]:  The
[1]:  quick
[2]:  brown
[3]:  fox
[4]:  jumps
[5]:  over
[6]:  the
[7]:  lazy
[8]:  dog

After sorting with the default comparer:
[0]:  brown
[1]:  dog
[2]:  fox
[3]:  jumps
[4]:  lazy
[5]:  over
[6]:  quick
[7]:  the
[8]:  The

After sorting with the reverse case-insensitive comparer:
[0]:  the
[1]:  The
[2]:  quick
[3]:  over
[4]:  lazy
[5]:  jumps
[6]:  fox
[7]:  dog
[8]:  brown


注:本文中的System.Collections.IComparer接口示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。