General Information
I have a ListView containing multiple columns. I have used a Microsoft Method to sort my ListView Columns. The ListView is populated by a SQL Query which correctly sorts Strings and Integers together (code shown below). For Example:
String & Integer Sorting Problem
The following JobNumber
strings are considered as sorted
"10", "1", "2", "3"
Using my SQL Query, they will become
"1", "2", "3", "10"
SQL String & Integer Sorter
Here is the Query I use to sort Strings and Integers correctly
SELECT PK_BillHeader, JobNumber, Description
FROM dbo.BillHeaders
ORDER BY Case IsNumeric(JobNumber)
When 1 Then Replicate('0', 50 - Len(JobNumber)) + JobNumber
Else JobNumber
End
This enters zeros until it gets the maximum length of my SQL column (50 chars) minus the JobNumber
's current length. That way, everything is considered as a string (including integers) which can then be sorted out correctly.
My Problem
When I click on the ListView Column Header (causing it to sort), it stops sorting the strings and integers correctly as it did with my SQL query. Instead, it sorts everything as a string which replicates my "String & Integer" sort problem once more ... What is more, if I have decimal values (second picture), it sorts them out pretty weird (see my compare code)
Compare Code
Public Function [Compare](ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim xItem As clsSortWrapper = CType(x, clsSortWrapper)
Dim yItem As clsSortWrapper = CType(y, clsSortWrapper)
Dim xText As String = xItem.sortItem.SubItems(xItem.sortColumn).Text
Dim yText As String = yItem.sortItem.SubItems(yItem.sortColumn).Text
If Decimal.TryParse(xText, vbNull) Then xText = xText.PadLeft(10, "0"c)
If Decimal.TryParse(yText, vbNull) Then yText = yText.PadLeft(10, "0"c)
Return xText.CompareTo(yText) * IIf(Me.ascending, 1, -1)
End Function
Is there a simpler way to acheive my result of sorting a ListView with Strings and Integers?
Note:
I omitted posting the ListView Sort code here because it would've clustered the post quite a bit. I provided the link which explains it thoroughly.