Link to home
Start Free TrialLog in
Avatar of tariqanis
tariqanis

asked on

Example needed for using Sortedlist

Hi,

The couple of books I read on VB.NET have omitted to explain what Sortedlists are and how to use them. I would be grateful for an explanation and a small clear example.
 
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

Heres is a sample for Listviews....

'FORM

   Private Sub Listview_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles Listview.ColumnClick
        ' Determine whether the column is the same as the last column clicked.
        If e.Column <> sortColumn Then
            ' Set the sort column to the new column.
            sortColumn = e.Column
            ' Set the sort order to ascending by default.
            lvwCustomers.Sorting = SortOrder.Ascending
        Else
            ' Determine what the last sort order was and change it.
            If lvwCustomers.Sorting = SortOrder.Ascending Then
                lvwCustomers.Sorting = SortOrder.Descending
            Else
                lvwCustomers.Sorting = SortOrder.Ascending
            End If
        End If
        ' Call the sort method to manually sort.
        lvwCustomers.Sort()
        ' Set the ListViewItemSorter property to a new ListViewItemComparer object.
        lvwCustomers.ListViewItemSorter = New ListViewItemComparer(e.Column, lvwCustomers.Sorting)
    End Sub

'CLASS

' Implements the manual sorting of items by columns.

Class ListViewItemComparer
    Implements IComparer
    Private col As Integer
    Private order As SortOrder

    Public Sub New()
        col = 0
        order = SortOrder.Ascending
    End Sub
    Public Sub New(ByVal column As Integer, ByVal order As SortOrder)
        col = column
        Me.order = order
    End Sub
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
                        Implements System.Collections.IComparer.Compare
        Dim returnVal As Integer = -1
        returnVal = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, _
                        CType(y, ListViewItem).SubItems(col).Text)
        ' Determine whether the sort order is descending.
        If order = SortOrder.Descending Then
            ' Invert the value returned by String.Compare.
            returnVal *= -1
        End If
        Return returnVal
    End Function
End Class

forgot to change....
 lvwCustomers to Listview
A SortedList stores entries as a Key/Value pair like a HashTable, but sorts the entries based upon the Key value.  (A HashTable stores Key/Value pair entries in the order they were inserted.)

Each entry placed into a SortedList must have a unique Key value.  Instead of retrieving values by Index as in an Array, you usually retrieve values by Key.  The value associated with a Key is an object, which means it can be another simple data type like a string, or a complex data type like an intance of a class.

Idle_Mind
Avatar of tariqanis
tariqanis

ASKER

Hi planocz, Idle_Mind

Thanks for both of you, but I need one of you to start out by showing me how to first create a sortedlist, fill it with data, and retrieve the data from it....



Idle_Mind started out with a good explanation, and I had hoped for some kind of illustration while planocz is showing an exmaple of how to use it without explaining first how to creat it and set out the goal for creating it.

Maybe I neglected to say I am a newbie at this, but I am.
ASKER CERTIFIED SOLUTION
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial