bubblesort algorithm not working in my combo box

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 Vjad96   User is offline

  • New D.I.C Head

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

bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 02:56 AM

     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




that is the actual code to sort the strings out

This is the other part of the code to get the string values:
  Dim ListOfString As New List(Of String)
        ListOfString.AddRange(cboReportTo.Items)
        ''Sort and format
        Dim outpt1 As String = ""
        For Each bub As String In BubbleSortString(ListOfString)
            outpt1 += bub & " "
        Next
        cboReportTo.Update()




I cannot get the array to add the range of items in that combo box and then organise it. I am unsure of that this code performs as i had got it from somewhere else which did not actually explain itself.

'For Each bub As String In BubbleSortString(ListOfString)
outpt1 += bub & " "
Next'

So my question is basically, How can I assign the combo box values into this array and then for it to perform using the bubblesort algorithm in the first set of code.

thank you for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: bubblesort algorithm not working in my combo box

#2 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 - 03:09 AM

You say algorithm but show code. Post your algorithm too. I do realize that bubble sort is easy to find online. However it will help on the exact algorithm you are following.

Well never mind the above. As you say the issue is with your combo box.

Reason for edit: Never mind

This post has been edited by GetSet: 22 March 2015 - 03:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 368
  • View blog
  • Posts: 1,575
  • Joined: 07-September 13

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 03:12 AM

I am not sure if the bubble sort routine is incorrect however you don't seems to be using it correct.

Please don't just copy and paste this code as I have not testing this code merely just written it.

    Public Sub SortList()

        Dim unsorted As New List(Of String)
        unsorted.AddRange(cboreportto.items)
        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
  • +
  • -

#4 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 368
  • View blog
  • Posts: 1,575
  • Joined: 07-September 13

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 03:21 AM

or if your not databinding it then:

  Public Sub SortList()

        Dim unsorted As New List(Of String)
        unsorted.AddRange(cboreportto.Items)
        cboreportto.Items.Clear()
        Dim sorted As List(Of String) = BubbleSortString(unsorted)

        For Each word As String In sorted
            word += " "
            cboreportto.Items.Add(word)
        Next

    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 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 - 03:34 AM

Unable to cast object of type 'ObjectCollection' to type 'System.Collections.Generic.IEnumerable`1[System.String]'. this seems to be the error which comes up, and I assume it's because of the 'cboReportto.item' part

any idea why that is?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,539
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 03:35 AM

You should preferably bind the ComboBox to a list (set its DataSource) and then just sort the list. However..
	    unsorted.AddRange(cboreportto.items)

This won't work directly, you need to convert the items-collection to an array:
        ListOfString.AddRange(cboReportTo.Items.OfType(Of String)().ToArray())

This
        For Each bub As String In BubbleSortString(ListOfString)
            outpt1 += bub & " "
        Next

just creates a string, which you do nothing with. Calling Update() on the ComboBox won't achieve anything, there is still no connection between the sorted list and the ComboBox.

Once you've sorted the items, obtaining a new list from your function, you can use this to re-populate your ComboBox with your new (sorted) list:
        cboReportTo.Items.Clear()
        cboReportTo.Items.AddRange(ListOfString.ToArray())

or you can Clear() the ComboBox, use For-Each to iterate your sorted list, adding each sorted-item to the ComboBox one at a time.

This post has been edited by andrewsw: 22 March 2015 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 368
  • View blog
  • Posts: 1,575
  • Joined: 07-September 13

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 03:38 AM

Thank you Andrew, Always forget to do that.
Was This Post Helpful? 0
  • +
  • -

#8 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 - 03:43 AM

I'm a bit confused here guys, I've done as you guys have recommended, but I still cannot get the combo box to update, How can I check that the function is even being called and that the list of items in the combo box has performed the process?
Was This Post Helpful? 0
  • +
  • -

#9 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 368
  • View blog
  • Posts: 1,575
  • Joined: 07-September 13

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 03:47 AM

Hey vjad96,

Taking Andrews advice I have modified my example:
I am taking it you are not data binding the combobox and therefore left that out.

 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.Items.AddRange(sorted.ToArray)

    End Sub


Was This Post Helpful? 1
  • +
  • -

#10 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 - 03:49 AM

Thanks for your help Andrew, it seems that I am databinding it as I am receiving the data from my database, I forgot to mention that, I am sorry, would you just replace cboreportto.Items.AddRange(sorted.ToArray) WITH cboreportto.datasource = sorted ?

thanks

This post has been edited by andrewsw: 22 March 2015 - 03:52 AM
Reason for edit:: Removed previous quote, just press the REPLY button

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,539
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 03:51 AM

View PostVjad96, on 22 March 2015 - 10:49 AM, said:

would you just replace cboreportto.Items.AddRange(sorted.ToArray) WITH cboreportto.datasource = sorted ?

thanks

Pretty much ;)
Was This Post Helpful? 0
  • +
  • -

#12 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 - 03:51 AM

Items collection cannot be modified when the DataSource property is set.

that is the error i get, and it points to the clear() process.
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,539
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 03:57 AM

Yes, if using a DataSource there is no need to use Clear() - the error tells you that you cannot use it anyway. Just setting the DataSource again will cause the ComboBox to be re-populated.

Of course, now that you are binding to a list, then bear in mind that the list has a Sort() method in-built.
Was This Post Helpful? 0
  • +
  • -

#14 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 - 03:59 AM

View Postandrewsw, on 22 March 2015 - 03:57 AM, said:

Yes, if using a DataSource there is no need to use Clear() - the error tells you that you cannot use it anyway. Just setting the DataSource again will cause the ComboBox to be re-populated.

Of course, now that you are binding to a list, then bear in mind that the list has a Sort() method in-built.


I had removed it but whenever I click the button which contains the sub procedure, the combo box contents are empty, I assume this is because there is nothing being stored in 'sorted'?
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,539
  • Joined: 12-December 12

Re: bubblesort algorithm not working in my combo box

Posted 22 March 2015 - 04:02 AM

Post your updated code.

Note that you do not have to quote the previous post, there is a REPLY button further down the page.

There is a debugging tutorial linked in my signature below. Take this and you could step through your code to see what is happening.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2