-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIntuitiveSortComparer.cs
40 lines (27 loc) · 982 Bytes
/
IntuitiveSortComparer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Gsemac.Win32.Native;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace Gsemac.Win32 {
/// <summary>
/// Sorts strings (i.e. file names) according to the "Intuitive or Numerical" sorting method used by Windows Explorer using <see cref="ShlwApi.StrCmpLogicalW"/>.
/// </summary>
public class IntuitiveSortComparer :
IComparer,
IComparer<string>,
IComparer<FileInfo> {
public int Compare(string x, string y) {
x = x ?? "";
y = y ?? "";
return ShlwApi.StrCmpLogicalW(x, y);
}
public int Compare(FileInfo x, FileInfo y) {
return Compare(x.FullName, y.FullName);
}
public int Compare(object x, object y) {
if (x is FileInfo fileInfoX && y is FileInfo fileInfoY)
return Compare(fileInfoX, fileInfoY);
return Compare(x.ToString(), y.ToString());
}
}
}