How would I sort a list of files by name to match how Windows Explorer displays them?

.net, c#, sorting

Solution

Windows Explorer uses an API called:

StrCmpLogicalW

to perform the sort in a "logical" manner.

Someone has also implemented a class in C# which will do this for you.

Problem

Let's say I've sorted a list of files in Explorer by name, like so: 2009-06-02-4.0.9.txt 2009-06-02-4.0.10.txt 2009-06-02-4.0.11.txt 2009-06-02-4.0.12.txt I have a FileInfo Comparer that sorts an array of FileInfo objects by name: ``` class FileInfoComparer : IComparer<FileInfo> { public int Compare(FileInfo x, FileInfo y) { return string.Compare(x.FullName, y.FullName, StringComparison.OrdinalIgnoreCase); } } ``` Sorting that same list of files from above using this Comparer yields: 2009-06-02-4.0.10.txt 2009-06-02-4.0.11.txt 2009-06-02-4.0.12.txt 2009-06-02-4.0.9.txt which is problematic, as the order is extremely important. I would imagine there's a way to mimic what Windows is doing in C# code, but I have yet to find a way. Any help is appreciated! Thanks!

Original source

Related problems