Results 1 to 2 of 2

Thread: [2005] Sorting Listview (Problem with ICompare)

  1. #1

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    [2005] Sorting Listview (Problem with ICompare)

    Hello,

    I wanted to sort my listview when I clicked on a column heading, and after reading the search on here, I found you need to use IComparer.

    I've managed to create a new class which does what I want apart from it treats dates as numbers and therefore sorts 1st December before 27th March.

    Can anyone help me with what I need to change in either my class or on the form ?

    Thanks,

    Class
    Code:
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    
    Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
    Dim item_y As ListViewItem = DirectCast(y, ListViewItem)
            
    Dim string_x As String
    If item_x.SubItems.Count <= m_ColumnNumber Then
        string_x = ""
    Else
        string_x = item_x.SubItems(m_ColumnNumber).Text
    End If
    
    Dim string_y As String
    If item_y.SubItems.Count <= m_ColumnNumber Then
        string_y = ""
    Else
         string_y = item_y.SubItems(m_ColumnNumber).Text
    End If
    
    If m_SortOrder = SortOrder.Ascending Then
      If IsNumeric(string_x) And IsNumeric(string_y) Then
           Return Val(string_x).CompareTo(Val(string_y))
      Else
           Return String.Compare(string_x, string_y)
      End If
    Else
      If IsNumeric(string_x) And IsNumeric(string_y) Then
           Return Val(string_y).CompareTo(Val(string_x))
      Else
           Return String.Compare(string_y, string_x)
      End If
    End If
    
    End Function
    Form
    Code:
    Dim new_sorting_column As ColumnHeader = lstvCurrentHolidays.Columns(e.Column)
    
    Dim sort_order As System.Windows.Forms.SortOrder
    If m_SortingColumn Is Nothing Then
         sort_order = SortOrder.Ascending
    Else
         If new_sorting_column.Equals(m_SortingColumn) Then
              If m_SortingColumn.Text.StartsWith("> ") Then
                  sort_order = SortOrder.Descending
              Else
                   sort_order = SortOrder.Ascending
              End If
         Else
              sort_order = SortOrder.Ascending
         End If
        m_SortingColumn.Text = _
        m_SortingColumn.Text.Substring(2)
    End If
    
    m_SortingColumn = new_sorting_column
    If sort_order = SortOrder.Ascending Then
         m_SortingColumn.Text = "> " & m_SortingColumn.Text
    Else
         m_SortingColumn.Text = "< " & m_SortingColumn.Text
    End If
    
    lstvCurrentHolidays.ListViewItemSorter = New ListViewComparer(e.Column, sort_order)
    lstvCurrentHolidays.Sort()

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] Sorting Listview (Problem with ICompare)

    vb Code:
    1. If TypeOf x Is Date Then
    2.             Dim date_x, date_y As Date
    3.             date_x = CDate(x)
    4.             date_y = CDate(y)
    5.             If date_x = date_y Then
    6.                 Return 0
    7.             ElseIf date_x > date_y Then
    8.                 Return 1
    9.             ElseIf date_x < date_y Then
    10.                 Return -1
    11.             End If
    12.         End If

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