Results 1 to 10 of 10

Thread: ListView: How to sort numbers?

  1. #1

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    Question ListView: How to sort numbers?

    The following code is working fine, but when it's storing numbers...

    VB Code:
    1. Friend Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     Private Sub ListView1_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
    4.         If e.Column <> sortColumn Then
    5.             sortColumn = e.Column
    6.             ListView1.Sorting = SortOrder.Ascending
    7.         Else
    8.             If ListView1.Sorting = SortOrder.Ascending Then
    9.                 ListView1.Sorting = SortOrder.Descending
    10.             Else
    11.                 ListView1.Sorting = SortOrder.Ascending
    12.             End If
    13.         End If
    14.         ListView1.Sort()
    15.         ListView1.ListViewItemSorter = New Comparer(e.Column, ListView1.Sorting)
    16.     End Sub
    17. End Class
    18. Friend Class Comparer
    19.     Implements IComparer
    20.     Private Col As Integer
    21.     Private Order As SortOrder
    22.     Public Sub New()
    23.         Col = 0
    24.         Order = SortOrder.Ascending
    25.     End Sub
    26.     Public Sub New(ByVal column As Integer, ByVal order As SortOrder)
    27.         Col = column
    28.         Me.Order = order
    29.     End Sub
    30.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    31.         Dim returnVal As Integer = -1
    32.         returnVal = String.Compare(CType(x, ListViewItem).SubItems(Col).Text, CType(y, ListViewItem).SubItems(Col).Text)
    33.         If Order = SortOrder.Descending Then returnVal *= -1
    34.         Return returnVal
    35.     End Function
    36. End Class
    For example it returns:

    1
    10
    2
    20

    Instead of:

    1
    2
    10
    20


    How to fix it?

    Thanks.

  2. #2
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168
    it treats as text


    use numbers as

    01
    02
    .
    .
    09
    10
    .
    .
    20

  3. #3

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    that is what u can call like a kind of bug..in alfabetic order its 1,10,2,20 and in numeric order 1,2,10,20 but as the listview works with text order then ur screwed. u'll have to recode a lot if u want to put it the right way
    \m/\m/

  5. #5
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    But the code is doing what you asked it to! Your Compare function is doing a string comparison.

    Use this Compare function instead to sort the list in numerical order.

    VB Code:
    1. Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
    2.                                 Implements System.Collections.IComparer.Compare
    3.             Dim item1 As ListViewItem = CType(x, ListViewItem)
    4.             Dim item2 As ListViewItem = CType(y, ListViewItem)
    5.  
    6.             Return Math.Sign(CLng(Val(item1.SubItems(mIndex).Text)) _
    7.                                     - CLng(Val(item2.SubItems(mIndex).Text)))
    8.         End Function

    If your list has mixed columns (some numeric and others text) then you will need to write two Comparer classes and use the appropriate one for each column.
    This world is not my home. I'm just passing through.

  6. #6

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Originally posted by trisuglow
    If your list has mixed columns (some numeric and others text) then you will need to write two Comparer classes and use the appropriate one for each column.
    Interesting.

    Do you know how to detect if it's number or text and then use the correct comparer?

    Thanks again.

  7. #7
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Here's an extract from my code. I assume you know what data-type is stored in each column?


    VB Code:
    1. Private Sub lvwShoppingList_ColumnClick(ByVal sender As Object, _
    2.                     ByVal e As System.Windows.Forms.ColumnClickEventArgs) _
    3.                                         Handles lvwShoppingList.ColumnClick
    4.  
    5.         Select Case e.Column
    6.  
    7.             Case 0, 1 'Product code, Description
    8.                 lvwShoppingList.ListViewItemSorter = _
    9.                                                 New CompareByString(e.Column)
    10.  
    11.             Case 2 'Loop A quantity
    12.                 lvwShoppingList.ListViewItemSorter = _
    13.                                             New CompareByQuantity(MXLoopA)
    14.  
    15.             Case 3 'Loop B quantity
    16.                 lvwShoppingList.ListViewItemSorter = _
    17.                                             New CompareByQuantity(MXLoopB)
    18.  
    19.             Case 4 'Loop C quantity
    20.                 lvwShoppingList.ListViewItemSorter = _
    21.                                             New CompareByQuantity(MXLoopC)
    22.  
    23.             Case 5 'Loop D quantity
    24.                 lvwShoppingList.ListViewItemSorter = _
    25.                                             New CompareByQuantity(MXLoopD)
    26.  
    27.             Case 6 'Total quantity
    28.                 lvwShoppingList.ListViewItemSorter = New CompareByQuantity(0)
    29.  
    30.  
    31.         End Select
    32.  
    33.     End Sub
    This world is not my home. I'm just passing through.

  8. #8

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Good one, but if don't we know it?

  9. #9
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    The items stored in each column (the subitems of your listviewitems) are data type Object. So, you can't really talk about them being text or numbers. It's up to you to decide how you want to sort them. I guess the only way to find out is to first loop through your list and do an IsNumeric() check on each item in the column to find out if they are all numbers.

    Does that help, or have I missed the point?
    This world is not my home. I'm just passing through.

  10. #10

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    That helped!

    Thanx.

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