Shaddow Posted January 12, 2004 Posted January 12, 2004 I have my listview implimented, working. colored to my hearts content.... working on sorting and have implimented a class called ListViewItemComparer. (as seen in MSDN, here and other places). the sorting works..... kind of.. it sorts based on the input being a string. I however want it to sort based on it being an integer. (ie. the following sort issue happens: 1, 2, 3, 4, 5, 6, 21, 33 results in: 1, 2, 21, 3, 33, 4, 5, 6 I am thinking that a solution falls into 1 or both of 2 categories. 1. possibly rewriting the ListviewItemComparer. and maybe funkifying some functions that will allow an 'extra' parameter to allow some specific casting or 2. I should beat my head against the table and then try someone 'elses' simpler solution.... anyone ? Thanks Quote
*Experts* Bucky Posted January 12, 2004 *Experts* Posted January 12, 2004 If it's not a problem to display the SubItems with excess 0's, just concatenate an extra 0 on the front of the single-digit values. Then it will sort correctly. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Administrators PlausiblyDamp Posted January 12, 2004 Administrators Posted January 12, 2004 What code do you have at he moment for the comparison? It may be quite easy to convert. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Shaddow Posted January 12, 2004 Author Posted January 12, 2004 Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _ Implements IComparer.Compare Return [string].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) End Function This is in the class. and what actually compares the two. Quote
Administrators PlausiblyDamp Posted January 13, 2004 Administrators Posted January 13, 2004 Instead of casting the items to strings could you not convert the subitems to integers and then do the comparison. not exactly sure of your requirements but something like the following may be a starting point. Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _ Implements IComparer.Compare Dim lv1, lv2 as ListViewItem lv1 = directcast(x, ListViewItem) lv2 = directcast(y, ListViewItem) if integer.parse(lv1.SubItems(col).Text) > integer.parse(lv2.SubItems(col).Text ) then return 1 if integer.parse(lv1.SubItems(col).Text) < integer.parse( lv2.SubItems(col).Text) then return -1 return 0 End Function Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Shaddow Posted January 13, 2004 Author Posted January 13, 2004 works fantastic. however (isn't there always a however?) this would invalidate any sorting by any other column that might actually be textual. Is there some way to overload the compare function? Quote
Administrators PlausiblyDamp Posted January 13, 2004 Administrators Posted January 13, 2004 Not that I'm aware of (doesn't mean there isn't). however you could check to see if x and y contain strings or integers and use the appropriate compare routines. if typeof x is string and typeof y is string then 'Do string compare end if if typeof x is integer and typeof y is integer then 'Do integer compare end if Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Shaddow Posted January 14, 2004 Author Posted January 14, 2004 this last part doesn't work. I believe it is because objects are passed not either integers or strings. i tried the code but It never executes. I think that its rather close to what I'll end up using. once I can do a quick determination on if the column is numeric or character based. Quote
Shaddow Posted January 16, 2004 Author Posted January 16, 2004 This seems to work. (I'm sure it wouldn't if the data processed wasn't integer(but still numeric).) however inelegant. I'll probably play with this a bit more to cover some more cases and a definite default to string if everything else doesn't work... Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _ Implements IComparer.Compare Dim lv1, lv2 As ListViewItem lv1 = DirectCast(x, ListViewItem) lv2 = DirectCast(y, ListViewItem) Dim holder1, holder2, comptype As String holder1 = lv1.SubItems(col).Text holder2 = lv2.SubItems(col).Text If IsNumeric(holder1) And IsNumeric(holder2) Then comptype = "int" Else comptype = "str" End If If comptype = "str" Then 'Do string compare Return [string].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) End If If comptype = "int" Then 'Do integer compare If Integer.Parse(lv1.SubItems(col).Text) > Integer.Parse(lv2.SubItems(col).Text) Then Return -1 If Integer.Parse(lv1.SubItems(col).Text) < Integer.Parse(lv2.SubItems(col).Text) Then Return 1 Return 0 End If ' End If End Function Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.