Mega Code Archive

 
Categories / VB.Net Tutorial / Collections
 

Sort the values in an Array using the default comparer and a custom comparer that reverses the sort order

Imports System Imports System.Collections Public Class SamplesArray    Public Class myReverserClass       Implements IComparer       Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare          Return New CaseInsensitiveComparer().Compare(y, x)       End Function    End Class    Public Shared Sub Main()       Dim myArr As [String]() =  {"The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog"}       Dim myComparer = New myReverserClass()       ' Sorts a section of the Array using the default comparer.       Array.Sort(myArr, 1, 3)       PrintIndexAndValues(myArr)       ' Sorts a section of the Array using the reverse case-insensitive comparer.       Array.Sort(myArr, 1, 3, myComparer)       PrintIndexAndValues(myArr)       ' Sorts the entire Array using the default comparer.       Array.Sort(myArr)       PrintIndexAndValues(myArr)      ' Sorts the entire Array using the reverse case-insensitive comparer.       Array.Sort(myArr, myComparer)       PrintIndexAndValues(myArr)    End Sub    Public Shared Sub PrintIndexAndValues(myArr() As [String])       Dim i As Integer       For i = 0 To myArr.Length - 1          Console.WriteLine("   [{0}] : {1}", i, myArr(i))       Next i    End Sub End Class