Home All Groups Group Topic Archive Search About

Sort List(of) like Explorer

Author
4 Jun 2009 12:08 PM
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

Author
4 Jun 2009 12:28 PM
nak
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-implementing-icomparer-for-fileinfo-objects-for-binary-searching-and-sorting/
    http://www.eggheadcafe.com/tutorials/aspnet/d5a8c475-2395-41e7-9c19-aa2edb23422e/custom-sorting-with-icomp.aspx

Nick.

Show quoteHide quote
"AGP" <sindizzy.***@softhome.net> wrote in message
news:iPOVl.31527$YU2.3738@nlpi066.nbdc.sbc.com...
> 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
>
>
Author
4 Jun 2009 1:09 PM
AGP
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

Show quoteHide quote
"nak" <a@a.com> wrote in message
news:E0EAC892-54A1-499F-9788-6931670BDA00@microsoft.com...
> 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-implementing-icomparer-for-fileinfo-objects-for-binary-searching-and-sorting/
>
> http://www.eggheadcafe.com/tutorials/aspnet/d5a8c475-2395-41e7-9c19-aa2edb23422e/custom-sorting-with-icomp.aspx
>
> Nick.
>
> "AGP" <sindizzy.***@softhome.net> wrote in message
> news:iPOVl.31527$YU2.3738@nlpi066.nbdc.sbc.com...
>> 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
>>
>>
Author
4 Jun 2009 1:15 PM
dunawayc
On Jun 4, 8:09 am, "AGP" <sindizzy.***@softhome.net> wrote:

> >> 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.
>

How does your sort differ from the the way Explorer sorts them?
Author
4 Jun 2009 2:15 PM
Andrew Morton
dunaw***@gmail.com wrote:
> On Jun 4, 8:09 am, "AGP" <sindizzy.***@softhome.net> wrote:
>
>>>> 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.
>>
>
> 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
Author
4 Jun 2009 2:22 PM
Andrew Morton
A little bit on how to use the same comparator (StrCmpLogicalW) as Windows
Explorer:
http://pinvoke.net/search.aspx?search=StrCmpLogicalW&namespace=[All]

Andrew
Author
4 Jun 2009 2:20 PM
nak
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.
Author
4 Jun 2009 4:07 PM
DIOS
On Jun 4, 9:20 am, "nak" <a***@a.com> wrote:
> 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
Author
4 Jun 2009 8:36 PM
DIOS
On Jun 4, 11:07 am, DIOS <sindi***@gmail.com> wrote:
Show quoteHide quote
> On Jun 4, 9:20 am, "nak" <a***@a.com> wrote:
>
> > 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

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
Author
5 Jun 2009 2:34 AM
James Hahn
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

"DIOS" <sindi***@gmail.com> wrote in message
news:e5812418-11b6-444b-8d7c-34f2d95f8550@l32g2000vba.googlegroups.com...
On Jun 4, 11:07 am, DIOS <sindi***@gmail.com> wrote:
> On Jun 4, 9:20 am, "nak" <a***@a.com> wrote:
>
> 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