bubblesort algorithm not working in my combo box

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1373 Views - Last Post: 22 March 2015 - 05:42 AM Rate Topic: -----

#16 Vjad96   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-March 15

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 04:14 AM

 Public Sub SortList()

        Dim unsorted As New List(Of String)

        unsorted.AddRange(cboReportTo.Items.OfType(Of String)().ToArray())

        'cboReportTo.Items.Clear()

        Dim sorted As List(Of String) = BubbleSortString(unsorted)

        For Each word As String In sorted

            word += " "
        Next

        cboReportTo.DataSource = sorted



    End Sub



   Public Function BubbleSortString(ByVal ListofString As List(Of String)) As List(Of String)
        Dim swaps As Boolean = True
        While swaps = True
            swaps = False
            For i As Integer = 0 To ListofString.Count - 2
                If ListofString(i) > ListofString(i + 1) Then
                    Dim temp As String = ListofString(i + 1)
                    ListofString(i + 1) = ListofString(i)
                    ListofString(i) = temp
                    swaps = True
                End If
            Next
        End While
        Return ListofString

    End Function


Was This Post Helpful? 0
  • +
  • -

#17 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,776
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 04:31 AM

That code works for me. What error do you get and on what line?

BTW This
        For Each word As String In sorted

            word += " "
        Next

doesn't achieve anything.
Was This Post Helpful? 0
  • +
  • -

#18 Vjad96   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-March 15

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 04:34 AM

I am not getting an error, I just cannot see the new sorted list in the combo box.

Could this be due to the fact I am firstly getting the items for the combo box from my database?

All it is doing wrong is replacing the combo box with no actual items.
Was This Post Helpful? 0
  • +
  • -

#19 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,776
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 04:37 AM

Take the debugging tutorial linked in my signature so that you could step through your code. Knowing how to using debugging tools is an essential skill.
Was This Post Helpful? 0
  • +
  • -

#20 GetSet   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 411
  • Joined: 08-February 11

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 04:44 AM

As andrewsw its at that point where you post your updated code with all commits. Why? Who knows what you thrown in.

Reason for edit: Code base to debug

We need a code base to debug. Without that chasing ghosts.

Reason for edit: programming language

The programming language and your source is the language. You could be speaking spanish or greek, wont matter. The code is the language. It contains the logic.

This post has been edited by GetSet: 22 March 2015 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#21 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,776
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 04:48 AM

For anyone following this, this code sets the DataSource of a ComboBox to a list (an array) of strings and then sorts these items on clicking a button.
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cboReportTo.DataSource = {"Pears", "Bananas", "Grapes", "Apples"}
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Call SortList()
    End Sub

    Public Function BubbleSortString(ByVal ListofString As List(Of String)) As List(Of String)
        Dim swaps As Boolean = True
        While swaps = True
            swaps = False
            For i As Integer = 0 To ListofString.Count - 2
                If ListofString(i) > ListofString(i + 1) Then
                    Dim temp As String = ListofString(i + 1)
                    ListofString(i + 1) = ListofString(i)
                    ListofString(i) = temp
                    swaps = True
                End If
            Next
        End While
        Return ListofString
    End Function

    Public Sub SortList()
        Dim unsorted As New List(Of String)

        unsorted.AddRange(cboReportTo.Items.OfType(Of String)().ToArray())
        Dim sorted As List(Of String) = BubbleSortString(unsorted)
        cboReportTo.DataSource = sorted
    End Sub

End Class

As mentioned earlier though, we can set the DataSource to a list (rather than an array) and use the Sort() method on the list.

This post has been edited by andrewsw: 22 March 2015 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#22 Vjad96   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-March 15

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 05:27 AM

could it be because of this you reckon?
cboReportTo.DisplayMember =
cboReportTo.ValueMember =

as i have these 2 assigned when i get the data from the database table?
Was This Post Helpful? 0
  • +
  • -

#23 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,776
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 05:37 AM

Yes. You are sorting a single-dimension list of strings, and then setting the DataSource to this list. The sorted list no longer has the DisplayMember and ValueMember as separate items.

The simple bubble sort is no longer appropriate. You need to sort the original list that you use as your DataSource. If the DataSource is a List<T> then you might add a sort method to your class T.

The approach you now need to take is very different to that discussed in the previous posts - because we weren't given, and don't have, the full picture. Your original post was about sorting a simple list of strings. This is not what you need to do. You need to sort a list of objects (essentially).
Was This Post Helpful? 0
  • +
  • -

#24 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,776
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 05:42 AM

Assuming that your list is called theList then you could do:
theList.Sort(Function(x, y) x.age.CompareTo(y.age))

or use the OrderBy extension method:
theList = theList.OrderBy(Function(x) x.age).ToList()

If your intention is to utilise, and demonstrate, a bubble sort then you could incorporate/create this as a method of your backing-class. That is, you could subclass List to add a new BubbleSort method.

This post has been edited by andrewsw: 22 March 2015 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2