|
-
Sep 5th, 2003, 04:29 PM
#1
Thread Starter
Addicted Member
ListView: How to sort numbers?
The following code is working fine, but when it's storing numbers...
VB Code:
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Sub ListView1_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
If e.Column <> sortColumn Then
sortColumn = e.Column
ListView1.Sorting = SortOrder.Ascending
Else
If ListView1.Sorting = SortOrder.Ascending Then
ListView1.Sorting = SortOrder.Descending
Else
ListView1.Sorting = SortOrder.Ascending
End If
End If
ListView1.Sort()
ListView1.ListViewItemSorter = New Comparer(e.Column, ListView1.Sorting)
End Sub
End Class
Friend Class Comparer
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)
If Order = SortOrder.Descending Then returnVal *= -1
Return returnVal
End Function
End Class
For example it returns:
1
10
2
20
Instead of:
1
2
10
20
How to fix it?
Thanks.
-
Sep 7th, 2003, 05:57 AM
#2
Addicted Member
it treats as text
use numbers as
01
02
.
.
09
10
.
.
20
-
Sep 7th, 2003, 08:01 PM
#3
Thread Starter
Addicted Member
-
Sep 8th, 2003, 01:58 AM
#4
yay gay
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/
-
Sep 8th, 2003, 04:36 AM
#5
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:
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)
Return Math.Sign(CLng(Val(item1.SubItems(mIndex).Text)) _
- CLng(Val(item2.SubItems(mIndex).Text)))
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.
-
Sep 8th, 2003, 08:11 AM
#6
Thread Starter
Addicted Member
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.
-
Sep 8th, 2003, 09:16 AM
#7
Here's an extract from my code. I assume you know what data-type is stored in each column?
VB Code:
Private Sub lvwShoppingList_ColumnClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ColumnClickEventArgs) _
Handles lvwShoppingList.ColumnClick
Select Case e.Column
Case 0, 1 'Product code, Description
lvwShoppingList.ListViewItemSorter = _
New CompareByString(e.Column)
Case 2 'Loop A quantity
lvwShoppingList.ListViewItemSorter = _
New CompareByQuantity(MXLoopA)
Case 3 'Loop B quantity
lvwShoppingList.ListViewItemSorter = _
New CompareByQuantity(MXLoopB)
Case 4 'Loop C quantity
lvwShoppingList.ListViewItemSorter = _
New CompareByQuantity(MXLoopC)
Case 5 'Loop D quantity
lvwShoppingList.ListViewItemSorter = _
New CompareByQuantity(MXLoopD)
Case 6 'Total quantity
lvwShoppingList.ListViewItemSorter = New CompareByQuantity(0)
End Select
End Sub
This world is not my home. I'm just passing through.
-
Sep 8th, 2003, 10:24 AM
#8
Thread Starter
Addicted Member
Good one, but if don't we know it?
-
Sep 9th, 2003, 03:13 AM
#9
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.
-
Sep 9th, 2003, 07:28 AM
#10
Thread Starter
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|