Array Reverse and Sort: Vbnet Arrays
Array Reverse and Sort: Vbnet Arrays
Console.WriteLine("Display myArray...") DisplayArray(myArray) Console.WriteLine("Reverse and display myArray...") Array.Reverse(myArray) DisplayArray(myArray) Dim myOtherArray As [String]( ) = _ {"e", "l", "s", "T", "o", "B", "f", "v"} Console.WriteLine("Display myOtherArray...") DisplayArray(myOtherArray) Console.WriteLine("Sort and display myOtherArray...") Array.Sort(myOtherArray) DisplayArray(myOtherArray) End Sub Public Shared Sub DisplayArray(ByVal theArray( Dim obj As Object For Each obj In theArray Console.WriteLine("Value: {0}", obj) Next obj Console.WriteLine(ControlChars.Lf) End Sub 'DisplayArray End Class
) As Object)
Array.Reverse(values) Demo
Imports System Public Class MainClass Shared Sub Main(ByVal args As String()) Dim values(10) As Integer For i As Integer = 0 To 10 values(i) = i Next i Array.Reverse(values) Dim txt As String = "" For i As Integer = 0 To 10 Console.WriteLine( values(i) ) Next i End Sub
End Class
'Reverse the order - elements will be in descending order Array.Reverse(strFriends) 'Enumerate the array For Each strName As String In strFriends 'Add the array item to the list System.Console.WriteLine(strName) Next End Sub End Class
Sort an Array
Imports System Public Class MainClass Shared Sub Main() 'Declare an array Dim strFriends(4) As String 'Populate the strFriends(0) strFriends(1) strFriends(2) strFriends(3) strFriends(4) array = "R" = "B" = "S" = "S" = "K"
Array.Sort(strFriends) 'Enumerate the array For Each strName As String In strFriends 'Add the array item to the list System.Console.WriteLine(strName) Next End Sub End Class Sort an Array and Use Array.Length properties Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) Dim friends(4) As String friends(0) friends(1) friends(2) friends(3) friends(4) = = = = = "1" "2" "3" "4" "5"
Array.Sort(friends) Dim upperBound As Integer = friends.Length - 1 Dim index As Integer For index = upperBound To 0 Step -1 Console.WriteLine(friends(index)) Next End Sub End Class