VB.Net/Data Structure/Array Sort

Материал из VB Эксперт
Версия от 12:45, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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(  ) As Object)
        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


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

Imports System
Public Class MainClass
    Shared Sub Main()
        "Declare an array
        Dim strFriends(4) As String
        "Populate the array
        strFriends(0) = "R"
        strFriends(1) = "B"
        strFriends(2) = "S"
        strFriends(3) = "S"
        strFriends(4) = "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 array
        strFriends(0) = "R"
        strFriends(1) = "B"
        strFriends(2) = "S"
        strFriends(3) = "S"
        strFriends(4) = "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) = "1"
        friends(1) = "2"
        friends(2) = "3"
        friends(3) = "4"
        friends(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