Sort list items in VB

I need to sort the items in a visual base list with numbers, i.e. I have a set of numbers that I would like to sort more often.

I tried just to use the Sorted listbox property, but found that it was handling the numbers as if they were strings, i.e. it would look at the first digit, then the second, etc., to determine the order. This means that 13 will show up to 5, for example.

I was thinking about dumping all numbers into an array, knowing the array and then pushing them back to the list, but to be honest, I don't know how to go for sorting. I decided that the array would be useless since listbox already acts as a pseudo-array.

Any ideas?

+3
source share
2 answers

You can use something like this:

Private Shared Sub SortIntegerListBox(ByVal listBox As ListBox)
    Dim TempList As New List(Of Integer)
    For Each LI In listBox.Items
        TempList.Add(Integer.Parse(LI.ToString()))
    Next
    TempList.Sort()
    listBox.DataSource = TempList
End Sub

And name it after binding:

    Dim Items As New List(Of Integer)
    Items.Add(1)
    Items.Add(13)
    Items.Add(2)

    Me.ListBox1.DataSource = Items
    SortIntegerListBox(Me.ListBox1)
+3
source

You can dump elements into an object List(Of Integer)and cause it to be sorted. Then link your list to this new list after sorting.

+1
source

Source: https://habr.com/ru/post/1730454/


All Articles