Results 1 to 3 of 3

Thread: Sorting Arrays before going into a combobox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    5

    Sorting Arrays before going into a combobox

    I need to sort the arrays in to alphabetical order the one holds products and the other the corresponding prices.

    Here is the code for the split
    Do While sr.Peek <> -1
    myArray = Split(sr.ReadLine, ",")
    cboProduct.Items.Add(myArray(0))
    PriceArray(counter) = myArray(1)
    counter = counter + 1
    Loop
    sr.Close()

    where would I put the sort?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you need to keep them synced up then that maybe be a bit of trouble (since they aren't in the same array), but you can easily sort an array with the sort method.

    VB Code:
    1. Dim names() As String = {"Dan", "Ed", "Mike", "Cookie", "Claire"}
    2.         Array.Sort(names)
    3.         ListBox1.Items.AddRange(names)

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is another example using an array of objects and sorting by an array of one of the field/properties.

    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim numbers() As Single = {1.5, 2, 5, 2.75, 3.3}
    3.         Dim items() As Object = {New DualList("Dan", 1.5), New DualList("Ed", 2), New DualList("Mike", 5), New DualList("Cookie", 2.75), New DualList("Calire", 3.3)}
    4.         Array.Sort(numbers, items)
    5.         ListBox1.Items.AddRange(items)
    6.     End Sub
    7.  
    8. End Class
    9.  
    10. Public Class DualList
    11.     Public Name As String
    12.     Public Number As Single
    13.  
    14.     Public Sub New(ByVal name As String, ByVal num As Single)
    15.         Me.Name = name
    16.         Me.Number = num
    17.     End Sub
    18.  
    19.     Public Overrides Function ToString() As String
    20.         Return Name & " {" & Number & "}"
    21.     End Function
    22. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width