Listview Sorter: Smarter data sorting required. Please help...
Hello all,
I am using a listview to show a bunch of data about some files. I use sorting code provided by Microsoft (http://support.microsoft.com/default...b;en-us;319399) to be able to sort on different columns of the listview. This code uses the IComparer. Sadly, the IComparer doesn't use the smarter 'Windows XP'-way of sorting.
With XP: 2.jpg will go before 10.jpg (because the number 2 is lower than the number 10)
With the IComparer: 10.jpg will go before 2.jpg (because the character 1 is 'lower' than character 2)
The IComparer only sorts textwise, not by value. Is there an easy way to programmatically sort by value? (So that series of 0...9-characters will be seen as values, in stead of normal text.)
Thanks in advance,
Alex.
Re: Listview Sorter: Smarter data sorting
If all the items in that column are numeric filenames with the ".jpg" or other extension then you could parse out the
extension and do a numeric ICompare.
VB Code:
'Usage:
'Me.ListView1.ListViewItemSorter = New ListViewItemComparer(ListView1.Text)
'Implements the manual sorting of items by index.
Friend Class ListViewItemComparer
Implements IComparer
Private ind As Integer
Public Sub New()
ind = 0
End Sub
Public Sub New(ByVal index As Integer)
ind = index
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim sNum1 As String
Dim sNum2 As String
sNum1 = DirectCast(x, ListViewItem).Text
sNum2 = DirectCast(y, ListViewItem).Text
'ToDo: Parse out the entensions
'Compare the numeric filename without the extensions
Return DirectCast(sNum1, Integer) - DirectCast(sNum2, Integer)
End Function
End Class
Re: Listview Sorter: Smarter data sorting required. Please help...
The problem is there's other columns too that need to be parsed.
There's four columns: ID (a number to keep them linked to a row in the DataSet I use), File name (like: ##.jpg or Album - ## - Song.mp3), Folder (like: C:\Files\) and File size (format: 1.000 KB, for filesize I could you the data from my database, where size is in bytes).
So, what I'm trying to say is that there's a lot of different types of input. So, the XP way of sorting (detecting text for numbers, then sorting on the value of those numbers it the preceding text is the same) would be best.
Re: Listview Sorter: Smarter data sorting required. Please help...
You could have a class for each column that is different sorting. In the column click you can set the sorting to each particular
ListViewItemSorter class depending on which column is clicked.