0% found this document useful (0 votes)
38 views

Array Reverse and Sort: Vbnet Arrays

This document contains several code examples demonstrating how to use arrays in VB.NET, including how to: - Reverse the order of elements in an array using Array.Reverse - Sort arrays in ascending order using Array.Sort - Print out array elements by iterating through the array - Access array properties like length
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Array Reverse and Sort: Vbnet Arrays

This document contains several code examples demonstrating how to use arrays in VB.NET, including how to: - Reverse the order of elements in an array using Array.Reverse - Sort arrays in ascending order using Array.Sort - Print out array elements by iterating through the array - Access array properties like length
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

VBNet Arrays

Array Reverse and Sort


Imports System Public Class MainClass Shared Sub Main() Dim myArray As [String](

) = {"W", "i", "A", "t"}

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

Sorts a pair of one-dimensional Array objects


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 myKeys As [String]() = {"red", "green", "yellow"} Dim myValues As [String]() = {"RED", "GREEN", "YELLOW"} Dim myComparer = New myReverserClass() PrintKeysAndValues(myKeys, myValues) Array.Sort(myKeys, myValues, 1, 3) PrintKeysAndValues(myKeys, myValues) End Sub Public Shared Sub PrintKeysAndValues(myKeys() As [String], myValues() As [String]) Dim i As Integer For i = 0 To myKeys.Length - 1 Console.WriteLine(" {0,-10}: {1}", myKeys(i), myValues(i)) Next i End Sub End Class

Reverse the order - elements will be in descending order


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"

'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

You might also like