-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNaturalSortComparer.cs
More file actions
178 lines (104 loc) · 4.99 KB
/
NaturalSortComparer.cs
File metadata and controls
178 lines (104 loc) · 4.99 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace Gsemac.Collections {
public class NaturalSortComparer :
IComparer,
IComparer<string>,
IComparer<FileInfo> {
// Public members
public NaturalSortComparer() :
this(CultureInfo.CurrentCulture) {
}
public NaturalSortComparer(CultureInfo cultureInfo) {
this.cultureInfo = cultureInfo;
}
public int Compare(string x, string y) {
// Each "part" of the string is compared separately, so need to break it up into pieces.
IEnumerator<string> partsX = GetParts(x).GetEnumerator();
IEnumerator<string> partsY = GetParts(y).GetEnumerator();
int compareResult;
while (partsX.MoveNext() && partsY.MoveNext()) {
// We only need one part to differ to decide how to order the strings.
if ((compareResult = CompareParts(partsX.Current, partsY.Current)) != 0)
return compareResult;
}
// If we get here, the strings are either equal, or one of them is null/empty.
return cultureInfo.CompareInfo.Compare(x, y, CompareOptions.IgnoreCase);
}
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());
}
// Private members
private readonly CultureInfo cultureInfo;
private bool IsCharNumeric(char input) {
return char.IsDigit(input);
}
private bool IsCharDelimiter(char input) {
// By treating the period as a delimiter, we can sort filenames as expected.
// http://archives.miloush.net/michkap/archive/2006/10/01/778990.html
return input == '.';
}
private bool ConsumeNumericSequence(string input, ref int startIndex, ref int endIndex) {
// Numeric sequences include all leading hyphens, which are ignored when comparing the numbers.
// Do not include trailing hyphens in the sequence.
bool consumedNonHyphenChar = false;
while (endIndex < input.Length) {
char nextChar = input[endIndex];
if (!IsCharNumeric(nextChar) && (nextChar != '-' || consumedNonHyphenChar))
break;
consumedNonHyphenChar = nextChar != '-';
++endIndex;
}
return startIndex != endIndex;
}
private bool ConsumeNonNumericSequence(string input, ref int startIndex, ref int endIndex) {
while (endIndex < input.Length) {
char nextChar = input[endIndex];
if (IsCharDelimiter(nextChar) || IsCharNumeric(nextChar) || nextChar == '-')
break;
++endIndex;
}
return startIndex != endIndex;
}
private IEnumerable<string> GetParts(string input) {
int startIndex = 0;
int endIndex = 0;
if (!string.IsNullOrEmpty(input)) {
while (startIndex < input.Length) {
if (ConsumeNumericSequence(input, ref startIndex, ref endIndex) || ConsumeNonNumericSequence(input, ref startIndex, ref endIndex)) {
yield return input.Substring(startIndex, endIndex - startIndex);
startIndex = endIndex;
}
else {
break;
}
}
}
}
private int CompareParts(string x, string y) {
if (int.TryParse(x.TrimStart('-'), NumberStyles.Integer, cultureInfo, out int intX) && int.TryParse(y.TrimStart('-'), NumberStyles.Integer, cultureInfo, out int intY)) {
int compareResult;
if ((compareResult = intX.CompareTo(intY)) != 0)
return compareResult;
// If two numbers compare equal but have a diffing number of leading 0s, the one with the most 0s comes first.
if ((compareResult = y.TrimStart('-').Length.CompareTo(x.TrimStart('-').Length)) != 0)
return compareResult;
// If the two numbers compare equal, and have the same number of leading zeros, a number with leading hyphens comes first.
// If there is more than one hyphen, the number with the least number of hyphens comes first.
return x.Length.CompareTo(y.Length);
}
else {
// Compare as strings.
// Comparisons are not case-sensitive.
return cultureInfo.CompareInfo.Compare(x, y, CompareOptions.IgnoreCase);
}
}
}
}