Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Created March 11, 2016 14:01
Show Gist options
  • Save Pzixel/ae9a2946f84db1a21c4d to your computer and use it in GitHub Desktop.
Save Pzixel/ae9a2946f84db1a21c4d to your computer and use it in GitHub Desktop.
public static class WinApi
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(string s1, string s2);
}
class AlphanumericComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return WinApi.StrCmpLogicalW(x, y);
}
public static AlphanumericComparer<T> For<T>(T obj, Func<T, string> func)
{
return new AlphanumericComparer<T>(func);
}
}
class AlphanumericComparer<T> : IComparer<T>
{
private readonly Func<T, string> _func;
public AlphanumericComparer(Func<T, string> func)
{
_func = func;
}
public int Compare(T x, T y)
{
return WinApi.StrCmpLogicalW(_func(x), _func(y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment