Sort List(of) like Explorer

  • Thread starter Thread starter AGP
  • Start date Start date
A

AGP

VB.NET 2005
I've been looking at some articles on how to sorta list but so far have not
got it to work properly. I have a List(of string) that I populate using the
GetFiles() function. The documenation states that the list is not guaranteed
to be in any order. What I would like is to have the order the same as that
used by Windows Explorer in WinXP. If you have a file list in Explorer and
have it to display in Details mode i can click on the file name header and
it will sort it ascending or descending. I have tried a few comparers but it
doesnt seem they match what Exploere uses for an algorithm. Any help or tips
appreciated.

AGP
 
Hi AGP,

Firstly you are only retrieving filenames, so that is the only field you
can sort by if you are using a List(Of String).

Turn the list into a List(Of FileInfo), that will give you a few more
fields to play with, such as date times, file size etc.

The create a custom IComparer and use that for your sorting, some
information at the following URLs,

http://dispo.se/2008/12/16/c-implem...nfo-objects-for-binary-searching-and-sorting/
http://www.eggheadcafe.com/tutorial...9-aa2edb23422e/custom-sorting-with-icomp.aspx

Nick.
 
Thanks for the tip. I only need filename and no other field. But again the
comparer im looking for should emulate what Windows Explorer sorter does
when you click on filename. All my files are from the same top level
directory so the main path should not matter. its the file names i want
ordered like in explorer. I know about custom Comparers, its the algorithm
that im trying to hone as the ones that ive used just dont give me the
result I want.

Thanks
AGP
 
How does your sort differ from the the way Explorer sorts them?

Windows Explorer will display 1.dat,2.dat,3.dat....10.dat in that order. The
usual string sort will display them as 1.data,10.dat,2.dat,3.dat...

Andrew
 
Hey AGP,

    Just use the StringComparer class

   http://msdn.microsoft.com/en-us/library/system.stringcomparer.aspx

    That should do what you want to do.

Nick.

ok i just used every stringcomparer available and they do not get
sorted like Explorer.

here is what explorer shows when I click on the file name header an
what I need:
N7A
N75B
N77B
N79B
N81B
N83B
N85A
N91B
N93B
N95B
N97B
N99A

here is what .NET gives me after using .Sort with all string
comparers:
N75B
N77B
N79B
N7A
N81B
N83B
N85A
N91B
N93B
N95B
N97B
N99A

So the Windows explorer sorting is what I'm after but cant seem to get
that from .NET.

AGP
 
ok i just used every stringcomparer available and they do not get
sorted like Explorer.

here is what explorer shows when I click on the file name header an
what I need:
N7A
N75B
N77B
N79B
N81B
N83B
N85A
N91B
N93B
N95B
N97B
N99A

here is what .NET gives me after using .Sort with all string
comparers:
N75B
N77B
N79B
N7A
N81B
N83B
N85A
N91B
N93B
N95B
N97B
N99A

So the Windows explorer sorting is what I'm after but cant seem to get
that from .NET.

AGP

This article seems to address the subject and it works. I'm going to
spend some time looking at what its doing but if there are any other
suggestions let me know.

http://www.codeproject.com/KB/recipes/NaturalComparer.aspx

AGP
 
This is a class that provides the sorting you are after for a ListView. It
is set up to sort on one of three columns, where the first is a filename
that needs to be in MS sequence, second is date (eg, created) and third is
size. It's probably a variation of the one you have located.

Imports System.Collections
Imports System.Windows.Forms

Public Class ListViewColumnSorter
Implements System.Collections.IComparer

Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitiveComparer

Public Sub New()
' Initialize the column to '0'.
ColumnToSort = 0
' Initialize the sort order to 'none'.
OrderOfSort = SortOrder.None
' Initialize the CaseInsensitiveComparer object.
ObjectCompare = New CaseInsensitiveComparer()
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
Dim compareResult As Integer
Dim listviewX As ListViewItem
Dim listviewY As ListViewItem
Dim DTX As DateTime
Dim DTY As DateTime
Dim b As Boolean
' Cast the objects to be compared to ListViewItem objects.
listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)
' Compare the two items.
Select Case ColumnToSort
' Calculate the correct return value based on the object comparison.
Case 0
compareResult = StrCmpLogicalW(listviewX.Text, listviewY.Text)
Case 1
b = DateTime.TryParse(listviewX.SubItems(ColumnToSort).Text, DTX)
b = DateTime.TryParse(listviewY.SubItems(ColumnToSort).Text, DTY)
compareResult = ObjectCompare.Compare(DTX, DTY)
Case 2
compareResult =
ObjectCompare.Compare(CLng(listviewX.SubItems(ColumnToSort).Text.Trim(","c)),
CLng _
(listviewY.SubItems(ColumnToSort).Text.Trim(","c)))
End Select
If (OrderOfSort = SortOrder.Ascending) Then
' Ascending sort is selected, return typical result of compare
operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Descending) Then
' Descending sort is selected, return negative result of compare
operation.
Return (-compareResult)
Else
' Return '0' to indicate that they are equal.
Return 0
End If
End Function

Public Property SortColumn() As Integer
Set(ByVal Value As Integer)
ColumnToSort = Value
End Set
Get
Return ColumnToSort
End Get
End Property

Public Property Order() As SortOrder
Set(ByVal Value As SortOrder)
OrderOfSort = Value
End Set
Get
Return OrderOfSort
End Get
End Property

<System.Runtime.InteropServices.DllImport("shlwapi.dll",
charset:=Runtime.InteropServices.CharSet.Unicode)> _
Public Shared Function StrCmpLogicalW(ByVal strA As String, ByVal strB As
String) As Int32
End Function
End Class

snip

So the Windows explorer sorting is what I'm after but cant seem to get
that from .NET.

AGP

This article seems to address the subject and it works. I'm going to
spend some time looking at what its doing but if there are any other
suggestions let me know.

http://www.codeproject.com/KB/recipes/NaturalComparer.aspx

AGP
 
Back
Top