copyright 1999 - 2014 by heinz prelle - hannover  - lenbachstraße 42 - www.visual-basic5.de | impressum
Beispiel: VB .Net - LINQ - Array sortieren

Public Class Form1

    Private Function SortArray(ByVal Index As Integer, ByVal ParamArray arr() As String) As List(Of String)
        Dim SortedArray As New List(Of String)
        Try
            Select Case Index
                Case 1
                    'Test 1
                    Dim Sorted = From Elements In arr Let Element = Elements.Split(" "c) Order By Element(0), CType(Element(1), Integer) Select Elements
                    Sorted = From Elements In arr Order By Elements.Split(" "c)(0), CInt(Elements.Split(" "c)(1))
                    For Each Element In Sorted
                        SortedArray.Add(Element)
                    Next
                Case 2
                    'Test 2
                    Dim Sorted = From Elements In arr Let Element = Elements.Split(" "c) Order By Element(0), CType(Element(1), Integer) Order By Element(0), CType(Element(2), Integer) Select Elements
                    Sorted = From Elements In arr Order By Elements.Split(" "c)(0), CInt(Elements.Split(" "c)(1)), CInt(Elements.Split(" "c)(2))
                    For Each Element In Sorted
                        SortedArray.Add(Element)
                    Next
                Case 3
                    'Test 3
                    Dim Sorted = From Elements In arr Let Element = Elements.Split(" "c) Order By Element(0), Element(1) Order By Element(0), CType(Element(2), Integer) Select Elements
                    Sorted = From Elements In arr Order By Elements.Split(" "c)(0), Elements.Split(" "c)(1), CInt(Elements.Split(" "c)(2))
                    For Each Element In Sorted
                        SortedArray.Add(Element)
                    Next
            End Select
        Catch ex As IndexOutOfRangeException
            MessageBox.Show(ex.Message(), "IndexOutOfRangeException")
        Catch ex As Exception
            MessageBox.Show(ex.Message(), "Exception")
        End Try
        Return SortedArray
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles MyBase.Load
        With Me
            .Text = "LINQ - Array sortieren - 3"
            .Button1.Text = "Test...1" : .Button2.Text = "Test...2" : .Button3.Text = "Test...3"
            .CenterToScreen()
        End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
        'Test 1
        Me.ListBox1.Items.Clear()
        Dim List As New List(Of String)(Me.SortArray(1, "B 4", "A 5", "Z 9", "B 4", "S 2", "A 2", "A 0", "S 1", "Q 7"))
        If Not List Is Nothing Then
            Me.ListBox1.Items.AddRange(List.ToArray())
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Test 2
        Me.ListBox2.Items.Clear()
        Dim List As New List(Of String)(Me.SortArray(2, "H 3 456", "H 6 132", "H 9 987", "H 3 509", "H 1 318", "H 8 587", "H 7 689", "H 1 433", "H 6 989"))
        If Not List Is Nothing Then
            Me.ListBox2.Items.AddRange(List.ToArray())
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'Test 3
        Me.ListBox3.Items.Clear()
        Dim List As New List(Of String)(Me.SortArray(3, "H CE 456", "B BG 132", "AA WW 987", "K AZ 509", "SPR UZ 318", "SFB RQ 587", "HH TR 689", "M HP 433", "D IO 989"))
        If Not List Is Nothing Then
            Me.ListBox3.Items.AddRange(List.ToArray())
        End If
    End Sub

End Class
Sourcecode Visual Basic