I have a listbox with data as follows;

78550Item10
78550Item6
78550Item4
78550Item5
78550Item3
78550Item9
78550Item2
78550Item1
78550Item8
78550Item7

How am I to sort the items in ascending order based on the digits at the end of the string so after sorting it is like;

78550Item1
78550Item2
78550Item3
78550Item4
78550Item5
78550Item6
78550Item7
78550Item8
78550Item9
78550Item10

I dont even know how to start this one.

thnx n advance

Recommended Answers

All 9 Replies

Hi,

A ListBox with its Sorted set to true should not be bound to data using the DataSource property. To display sorted data in a bound ListBox, you should bind to a data source that supports sorting and have the data source provide the sorting.

Isnt it possible in any other way like taking the last digit and compare and sort according to the compare result?

If you have the listview in details view you can define a custom sort. Let's say your listview is called lvwList

lvwList.ListViewItemSorter = New MyCustomSorter

But first you have to write the MyCustomSorter class. This is code that will compare two of your items and determine whether one is <, =, or > to the other

Class MyCustomSorter

Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

    Dim item1, item2 As ListViewItem
    Dim text1, text2 As String

    item1 = CType(x, ListViewItem)
    item2 = CType(y, ListViewItem)

    text1 = item1.SubItems(0).Text.SubString(9)
    text2 = item2.SubItems(0).Text.SubString(9)

    If text1 > text2 Then Return 1
    If text1 < text2 Then Return -1
    Return 0

End Function

It's up to you to determine how to compare the items. I just did a substring as an example. You may have to get fancier to extract the strings. Also, for my example to work you would have to convert text1 and text2 to numeric and compare those.

Member Avatar for Unhnd_Exception
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim Strings() As String = {"78550Item10", "78550Item6", "78550Item4", "78550Item5", "78550Item3", "78550Item9", "78550Item2", "78550Item1", "78550Item8", "78550Item7"}
        ListBox1.Items.AddRange(Strings)

        SortListBox()

    End Sub

    Private Sub SortListBox()
        'Uses linq to sort the box.
        'Items must be in the format of xxxxxItemx
        ListBox1.BeginUpdate()

        'Create a copy of the listbox items
        Dim Items(ListBox1.Items.Count - 1) As String
        ListBox1.Items.CopyTo(Items, 0)

        'Sort the items by the last digits
        Dim Sorted = From Item In Items _
                     Order By CInt(CStr(Item).Substring(9)) Ascending

        'Clear out the old and add the new
        ListBox1.Items.Clear()
        ListBox1.Items.AddRange(Sorted.ToArray)

        ListBox1.EndUpdate()

    End Sub

Make sure you have ListView1 in Details mode and that it has one (or more) columns in its collection.

Public Class Form1

    Class MyCustomSorter

        Implements IComparer

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

            Dim item1 As ListViewItem = CType(x, ListViewItem)
            Dim item2 As ListViewItem = CType(y, ListViewItem)

            Dim num1 As Integer = Int(item1.SubItems(0).Text.Substring(9))
            Dim num2 As Integer = Int(item2.SubItems(0).Text.Substring(9))

            If num1 > num2 Then Return 1
            If num1 < num2 Then Return -1
            Return 0

        End Function

    End Class

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim s As String
        Dim strings() As String = {"78550Item10", "78550Item6", "78550Item4", "78550Item5", "78550Item3", "78550Item9", "78550Item2", "78550Item1", "78550Item8", "78550Item7"}

        For Each s In strings
            ListView1.Items.Add(s)
        Next

        ListView1.ListViewItemSorter = New MyCustomSorter
        ListView1.Sorting = SortOrder.Ascending

    End Sub

End Class
Member Avatar for Unhnd_Exception

@Reverend Jim. Thats a nice IComparer you got but I think hes talking about a ListBox and not a ListView.

Member Avatar for Unhnd_Exception

I should also say If I were the poster I would change out the list box to a list view and use Reverend Jim's Prescription of the IComparer.

DOH! I do believe you are right. In my haste to help I overlooked that little fact. Ah well, as you so correctly stated, change ListBox to ListView and everything should work.

thanq guys, Reverend Jim's code did work when i changed to a listview, thank you all :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.