views:

62

answers:

2

I'm killing myself and dehydrating trying to get this array to sort.

I have an array containing directories generated by;

Dim Folders() As String = Directory.GetDirectories(RootPath)

I need them to be sorted so they appear like in windows explorer in win7 / vista. -- numerically and alphabetically by folder names.

The folder names contain both letters and numbers, sometimes letters only or numbers only.

The simple Array.Sort(Folders) results in

C:\inetpub\wwwroot\rootpath\1
C:\inetpub\wwwroot\rootpath\10
C:\inetpub\wwwroot\rootpath\100
C:\inetpub\wwwroot\rootpath\1004
C:\inetpub\wwwroot\rootpath\101
C:\inetpub\wwwroot\rootpath\11
C:\inetpub\wwwroot\rootpath\12
C:\inetpub\wwwroot\rootpath\2
C:\inetpub\wwwroot\rootpath\3
C:\inetpub\wwwroot\rootpath\4
C:\inetpub\wwwroot\rootpath\5
C:\inetpub\wwwroot\rootpath\6
C:\inetpub\wwwroot\rootpath\7
C:\inetpub\wwwroot\rootpath\8
C:\inetpub\wwwroot\rootpath\87skjnd
C:\inetpub\wwwroot\rootpath\89sdf93kmw3
C:\inetpub\wwwroot\rootpath\9
C:\inetpub\wwwroot\rootpath\ad
C:\inetpub\wwwroot\rootpath\bin
C:\inetpub\wwwroot\rootpath\dark
C:\inetpub\wwwroot\rootpath\erk
C:\inetpub\wwwroot\rootpath\jkh23978yoaslkd3
C:\inetpub\wwwroot\rootpath\lk2309as
C:\inetpub\wwwroot\rootpath\work
C:\inetpub\wwwroot\rootpath\zone

What I want to have (and what windows explorer displays) is ...

C:\inetpub\wwwroot\rootpath\1
C:\inetpub\wwwroot\rootpath\2
C:\inetpub\wwwroot\rootpath\3
C:\inetpub\wwwroot\rootpath\4
C:\inetpub\wwwroot\rootpath\5
C:\inetpub\wwwroot\rootpath\6
C:\inetpub\wwwroot\rootpath\7
C:\inetpub\wwwroot\rootpath\8
C:\inetpub\wwwroot\rootpath\9
C:\inetpub\wwwroot\rootpath\10
C:\inetpub\wwwroot\rootpath\11
C:\inetpub\wwwroot\rootpath\12
C:\inetpub\wwwroot\rootpath\87skjnd
C:\inetpub\wwwroot\rootpath\89sdf93kmw3
C:\inetpub\wwwroot\rootpath\100
C:\inetpub\wwwroot\rootpath\101
C:\inetpub\wwwroot\rootpath\1004
C:\inetpub\wwwroot\rootpath\ad
C:\inetpub\wwwroot\rootpath\bin
C:\inetpub\wwwroot\rootpath\dark
C:\inetpub\wwwroot\rootpath\erk
C:\inetpub\wwwroot\rootpath\jkh23978yoaslkd3
C:\inetpub\wwwroot\rootpath\lk2309as
C:\inetpub\wwwroot\rootpath\work
C:\inetpub\wwwroot\rootpath\zone

I googled and found out that a class needs to be written that uses IComparable to sort the elements. Being a supernewbie ... I really don't know how it can be done. Most examples I looked at had multi dimensional arrays and keys :S ...

would be even nicer if the sorting could be applied to an array of filenames (instead of foldernames) or an array containing both folders and files ... in which case the sorted folders appear at the top and sorted files below ... is this even possible?

Any help will be greatly appriciated ... :D thanks.

A: 

Array.Sort has an IComparer parameter too, you can override sorting behavior if you don't like the default. see Array.Sort Method (T[], IComparer) how to do it

ArsenMkrt
+4  A: 

You would need to implement an IComparer, as opposed to creating a class that implements IComparable. The difference is that an IComparer has the necessary "knowledge" to compare two objects whereas IComparable is implemented by a class that knows how to compare itself against something else.

And the way Windows Explorer sorts filenames is using a function called StrCmpLogicalW. You can use this function in your own IComparer to get the same sort behavior as Windows Explorer. This function treats numeric parts of strings as numbers so that 1 sorts before 10.

public class MyComparer : IComparer<string> {

    [DllImport("shlwapi.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
    static extern int StrCmpLogicalW(String x, String y);

    public int Compare(string x, string y) {
        return StrCmpLogicalW(x, y);
    }

}

Array.Sort(unsortedNames, new MyComparer());

And since I just noticed the question is tagged VB... Forgive my rusty VB!

Public Class MyComparer
    Implements IComparer(Of String)

    Declare Unicode Function StrCmpLogicalW Lib "shlwapi.dll" ( _
        ByVal s1 As String, _
        ByVal s2 As String) As Int32

    Public Function Compare(Byval x as String, Byval y as String) As Integer _
        Implements System.Collections.Generic.IComparer(Of String).Compare

        Return StrCmpLogicalW(x, y)

    End Function

End Class
Josh Einstein
Wow .... works really well ... thanks Josh ! ...Just one concern though ... what I'm working on will be sold as a part of a product. Now will this library be available on all servers running ASP.Net 2.0+ .. sorry if the question sounds stupid. Just wanted to make sure I won't run into problems in cases where this library is not available. Is it possible to check if this library exists and then apply the sorting ... and skip if it doesn't exist.Thank you so much Josh ... would've taken me years to figure this one out by myself.
Norman
According to the doc page it's supported on XP and up, and Server 2003 and up. For down-level platforms I suppose you could swap in an implementation that resorted to plain alpha sorting.
Josh Einstein
There was a small problem in the code (possibly because of conversion to VB) Line 8 works if it is ;Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare--- Is there a way to check if the library exists? Ignore if you think this was a stupid questionThanks,Norman.
Norman
Sorry Norman I wasn't trying to ignore. I think you can use the GetProcAddress API to determine if the function exists but I'm not really a PInvoke expert. Thanks for the heads up about the glitch in the VB snippet. I'll update it.
Josh Einstein