5

I have a 2X50 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 array, that also will help me.

14
  • What do the values actually mean? Would it make more sense to use a class? Commented Apr 3, 2017 at 7:25
  • The second column of the array (If we consider that like a table) holds only integers. I want to sort the array in descending order of the values of the 2nd column Commented Apr 3, 2017 at 7:28
  • So what's the first column? Commented Apr 3, 2017 at 7:28
  • That holds string, if changing the datatype to integer will allow me to sort easily, I can do that. Commented Apr 3, 2017 at 7:30
  • The type is irrelevant.. Is it a unique identifier? If so, then make a class and you can just sort on a property Commented Apr 3, 2017 at 7:31

3 Answers 3

7

I would recommend the use of a List(Of Tuple) instead of an array. It is more dynamic. Please check this code:

Sub SortList()
    'Declare the List Of Tuple with a Tuple of Char, Integer, Integer
    Dim lstToSort As New List(Of Tuple(Of Char, Integer, Integer))
    'Example to Add items
    lstToSort.Add(Tuple.Create("R"c, 250645, 11))
    lstToSort.Add(Tuple.Create("C"c, 125212, 25))
    'Sort is just 1 line
    lstToSort = lstToSort.OrderBy(Function(i) i.Item2).ToList
    'Loop through the elements to print them
    For Each tpl As Tuple(Of Char, Integer, Integer) In lstToSort
        Console.WriteLine(tpl.Item1 & "-" & tpl.Item2 & "," & tpl.Item3)
    Next
End Sub

Edit: Given your edit on the question here is the code fixed:

Sub SortList()
    'Declare the List Of Tuple with a tuple of String, Integer
    Dim lstToSort As New List(Of Tuple(Of String, Integer))
    'Example to add items
    lstToSort.Add(Tuple.Create("R-250645", 11))
    lstToSort.Add(Tuple.Create("C-125212", 25))
    'Sort is just 1 line
    lstToSort = lstToSort.OrderBy(Function(i) i.Item2).ToList
    'Loop through the elements to print them
    For Each tpl As Tuple(Of String, Integer) In lstToSort
        Console.WriteLine(tpl.Item1 & "," & tpl.Item2)
    Next
End Sub

Give it a try and let me know your comments

Sign up to request clarification or add additional context in comments.

3 Comments

I am sure this will work. But I will use the previous answer in my solution as that provides me some secondary benefits.
This worked perfectly for a similar problem that I had!
@JerryT glad it helped you
6

There are many possible solutions to your question, but in my experience the best is to use a System.Data.DataTable:

Dim dtb As New System.Data.DataTable
dtb.Columns.Add("Column1")
dtb.Columns.Add("Column2", GetType(Integer))
dtb.Rows.Add("Z-123456", 2)
dtb.Rows.Add("R-125212", 11)
dtb.Rows.Add("C-254645", 25)
dtb.Rows.Add("R-456598", 96)
dtb.Rows.Add("M-456878", 35)
dtb.Rows.Add("O-980857", 89)
Dim dvw As DataView = dtb.DefaultView
dvw.Sort = "Column2 ASC"
Dim dtbSorted As DataTable = dvw.ToTable()
DataGridView1.DataSource = dtbSorted

5 Comments

It is not best to use a DataTable unless you're reading the data directly into it from a database. If you can otherwise populate a DataTable then you can populate a collection of instances of a dedicated type.
@jmcilhinney - yes, it's possible to create custom classes and implement IComparable etc but DataTable is much, much, quicker and easier to code.
Why would you need to implement IComparable? The sorting is to be done by either a String or Integer property and both already implement IComparable. You don't even need a custom class if you don't want, thanks to Tuple. The code to create a sort a List(Of Tuple(Of String, Integer)) will be simpler than using a DataTable.
By the way, your example of sorting a text column containing numbers will only work if each value is the same length. As you have it, "10" would come before "9" in the sort.
Ultimately I guess there are many ways to solve this problem. You are right about integer sorting - DataTable does support most data types for its columns (even custom types/classes IIRC), I'll update my answer.
0

Code:

Public Function Sort2DimArray(SA As Array, order As Boolean, sc0 As Integer, Optional sc1 As Integer = -1, Optional sc2 As Integer = -1) As Array

    Dim cols As Integer = SA.GetLength(1) - 1
    Dim rows As Integer = SA.GetLength(0) - 1
    Dim na(rows, cols) As String
    Dim a(rows) As String
    Dim b(rows) As Integer
    Dim c As Integer = 1
    If sc1 > -1 Then c = c + 1
    If sc2 > -1 Then c = c + 1

    For x = 0 To rows
        If c = 1 Then a(x) = SA(x, sc0)
        If c = 2 Then a(x) = SA(x, sc0) & SA(x, sc1)
        If c = 3 Then a(x) = SA(x, sc0) & SA(x, sc1) & SA(x, sc2)
        b(x) = x
    Next
    Array.Sort(a, b)
    If order = False Then
        For x = 0 To rows
            For y = 0 To cols
                na(x, y) = SA(b(x), y)
            Next
        Next
    Else
        For x = 0 To rows
            For y = 0 To cols
                na(rows - x, y) = SA(b(x), y)
            Next
        Next
    End If
    Sort2DimArray = na
End Function

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.