none
Sort Multidimensional Array in VB.NET

    Question

  •  have a 50X2 array like this-

    R-125212,11
    C-254645,25
    R-456598,96
    M-456878,35
    O-980857,89
    And so on...

    Now I want to sort this array with the values of the 2nd Column. So the output should look like-

    R-125212,11
    C-254645,25
    M-456878,35
    O-980857,89
    R-456598,96
    And so on...
    How to do this with VB.NET easily? If there is other better way to have the similar result without using <g class="gr_ gr_10 gr-alert gr_gramm gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="10" id="10">array</g>, that also will also help me.






    Monday, April 3, 2017 7:20 AM

All replies

  • Sayom,

    This isn't what you asked for but maybe it'll give you some ideas:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim data As New List(Of Sayom) With data .Add(New Sayom(-125212, 11)) .Add(New Sayom(-254645, 25)) .Add(New Sayom(-456598, 96)) .Add(New Sayom(-456878, 35)) .Add(New Sayom(-980857, 89)) End With Dim sorted As IEnumerable(Of Sayom) = _ Sayom.GetSorted(data, Sayom.SortBy.ColumnB, _ Sayom.SortOrder.Ascending) Dim sb As New System.Text.StringBuilder For Each s As Sayom In sorted sb.AppendLine(String.Format("{0},{1}", s.ColumnA, s.ColumnB)) Next MessageBox.Show(sb.ToString) Stop End Sub End Class Public Class Sayom Public Enum SortBy ColumnA ColumnB End Enum Public Enum SortOrder Ascending Descending End Enum Private _columnA As Integer Private _columnB As Integer Public Sub New(ByVal valA As Integer, _ ByVal valB As Integer) _columnA = valA _columnB = valB End Sub Public Shared Function _ GetSorted(ByVal enumerable As IEnumerable(Of Sayom), _ ByVal columnSort As SortBy, _ ByVal order As SortOrder) As IEnumerable(Of Sayom) Dim retVal As IEnumerable(Of Sayom) = Nothing If enumerable IsNot Nothing AndAlso enumerable.Count > 0 Then Dim qry As System.Linq.IOrderedEnumerable(Of Sayom) = Nothing Select Case columnSort Case SortBy.ColumnA Select Case order Case SortOrder.Ascending qry = From s As Sayom In enumerable Order By s.ColumnA Ascending Case SortOrder.Descending qry = From s As Sayom In enumerable Order By s.ColumnA Descending End Select Case SortBy.ColumnB Select Case order Case SortOrder.Ascending qry = From s As Sayom In enumerable Order By s.ColumnB Ascending Case SortOrder.Descending qry = From s As Sayom In enumerable Order By s.ColumnB Descending End Select End Select If qry IsNot Nothing Then retVal = qry.ToArray End If End If Return retVal End Function Public ReadOnly Property ColumnA As Integer Get Return _columnA End Get End Property Public ReadOnly Property ColumnB As Integer Get Return _columnB End Get End Property End Class



    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, April 3, 2017 1:31 PM
  • Hi

    A bit late, but here is some code I reached using another post in this forum by Xiaoyun Li – MSFT April 2009

    Needs ListBox1 and ListBox2 on a Form1

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        ' from https://social.msdn.microsoft.com/Forums/en-US/6961386f-d4b5-473c-9865-1ea093ccc281/how-do-i-sort-a-multidimensional-array?forum=Vsexpressvb
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ShowArray(ListBox1)
            Array.Sort(jaggedarr, myComparer)
            ShowArray(ListBox2)
        End Sub
        Private Sub ShowArray(lb As ListBox)
            lb.Items.Clear()
            For i As Integer = 0 To jaggedarr.Length - 1
                lb.Items.Add(jaggedarr(i)(0).ToString & ", " & jaggedarr(i)(1).ToString)
            Next
        End Sub
    
        Dim intarr1() As String = {"R-125212", "11"}
        Dim intarr2() As String = {"C-254645", "25"}
        Dim intarr3() As String = {"R-456598", "96"}
        Dim intarr4() As String = {"M-456878", "35"}
        Dim intarr5() As String = {"O-980857", "89"}
        Dim intarr6() As String = {"O-980857", "1"}
        Dim jaggedarr()() As String =
        {intarr1, intarr2, intarr3, intarr4, intarr5, intarr6}
        Dim myComparer As New JaggedComparer
    
        Class JaggedComparer
            Implements IComparer
            Public Function Compare(ByVal x As Object, ByVal y As Object) _
            As Integer Implements IComparer.Compare
                ' x and y are arrays of strings
                ' sort on the 2nd item in each array
                Dim a1() As String = DirectCast(x, String())
                Dim a2() As String = DirectCast(y, String())
                Return a1(1).CompareTo(a2(1))
            End Function
        End Class
    
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Monday, April 3, 2017 2:56 PM
    Monday, April 3, 2017 2:52 PM
  • If there is other better way to have the similar result without using class, that also will also help me.

    Select the algorithm of your choice from any of those available. For instance:
    http://www.codeguru.com/vb/gen/vb_misc/algorithms/article.php/c14627/Sorting-Algorithms-In-VB.htm
    http://www.vb-helper.com/tut1.htm

    At any line where the item to be sorted is referenced, use the second dimension. At any line where the array element is moved, repeat the line, referring to the first array dimension instead of the second.

    For instance:

                If List(i - 1) > List(i) Then
                    ' See where to drop the bubble.
                    tmp = List(i - 1)
    
     would become
                If List(i - 1, 1) > List(i, 1) Then
                    ' See where to drop the bubble.
                    tmp = List(i - 1, 1)
                    tmp1 = List(i - 1, 0)
    Monday, April 3, 2017 9:42 PM
  • Hi Sayom,

    I suggest you to add data in the DataTable , sort according to the second column of the table.

    Dim dt As New DataTable
            dt.Columns.Add("Column1")
            dt.Columns.Add("Column2")
            dt.Rows.Add("R-125212", "11")
            dt.Rows.Add("C-254645", "25")
            dt.Rows.Add("R-456598", "96")
            dt.Rows.Add("M-456878", "35")
            dt.Rows.Add("O-980857", "89")
            Dim dfv As DataView = dt.DefaultView
            dfv.Sort = "Column2 ASC"
            Dim dtnewAs DataTable = dfv.ToTable()
    

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 5, 2017 2:09 AM
    Moderator
  • Hi Sayom,

    Please remember to close your thread by marking helpful post as answer, it is very beneficial to the other communities who have the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 12, 2017 7:32 AM
    Moderator