Improve performance of sorting files by extension

Posted by DxCK on Stack Overflow See other posts from Stack Overflow or by DxCK
Published on 2010-05-20T21:19:01Z Indexed on 2010/05/20 21:20 UTC
Read the original article Hit count: 270

Filed under:
|
|
|
|

With a given array of file names, the most simpliest way to sort it by file extension is like this:

Array.Sort(fileNames,
    (x, y) => Path.GetExtension(x).CompareTo(Path.GetExtension(y)));

The problem is that on very long list (~800k) it takes very long to sort, while sorting by the whole file name is faster for a couple of seconds!

Theoretical, there is a way to optimize it: instead of using Path.GetExtension() and compare the newly created extension-only-strings, we can provide a Comparison than compares starting from the LastIndexOf('.') without creating new strings.

Now, suppose i found the LastIndexOf('.'), i want to reuse native .NET's StringComparer and apply it only to the part on string after the LastIndexOf('.'). Didn't found a way to do that.

Any ideas?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#