how do i arrange the items in the list box in ascending order ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gerald07
    New Member
    • Aug 2010
    • 3

    how do i arrange the items in the list box in ascending order ?

    Code:
     For b = 1 To 5
                a(b) = InputBox("Input Grade")
                ListBox1.Items.Add(a(b))
            Next
            ListBox1.Sorted = True
    i input(98, 100, 93 ,94,96)

    then it displays ( 100,93,94,96,98 )

    but it should be (100,98,96,94,9 3)

    thanks in advance.
  • Kalen Viljoen
    New Member
    • Feb 2011
    • 12

    #2
    I think you have to code it somehow. Eg:

    Code:
    Dim List As New List(Of Integer)
    
            'Add ListBox Items to List
            For Each ListBoxItem In ListBox1.Items
                List.Add(ListBoxItem)
            Next
    
            'Sort the List and Clear the ListBox
            List.Sort()
            ListBox1.Items.Clear()
    
            'Add to ListBox Ascending
            For Each ListItem In List
                ListBox1.Items.Add(ListItem)
            Next
    
            'Add to ListBox Descending
            For i = List.Count - 1 To 0 Step -1
                ListBox1.Items.Add(List(i))
            Next

    Comment

    • R MacDonald
      New Member
      • Mar 2011
      • 24

      #3
      And when using Kalen's method, don't forget to remove the line:

      "ListBox1.Sorte d = True

      from your code, or you'll be back where you started. Note that the Sorted list box is sorting according to expectation. The ListBox entries are strings (not numbers) and they are being sorted in alphabetic order.

      Cheers,
      Randy

      Comment

      Working...