-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringLogicalComparer.cs
207 lines (184 loc) · 4.65 KB
/
StringLogicalComparer.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections;
using System.Windows.Forms;
namespace TheraWii
{
class ListComparer : IComparer
{
private int col;
public ListComparer()
{
col = 0;
}
public ListComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
int c = StringLogicalComparer.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
if (((ListViewItem)x).ListView.Sorting == SortOrder.Descending)
{
return c * -1;
}
return c;
}
}
public sealed class StringLogicalComparer : IComparer
{
private static readonly IComparer _default = new StringLogicalComparer();
private static readonly IComparer _defaultZeroesFirst = new StringLogicalComparer(true);
private bool zeroesFirst = false;
private StringLogicalComparer()
{ }
private StringLogicalComparer(bool zeroesFirst)
{
this.zeroesFirst = zeroesFirst;
}
public static IComparer Default
{
get { return _default; }
}
public static IComparer DefaultZeroesFirst
{
get { return _defaultZeroesFirst; }
}
public int Compare(object x, object y)
{
if (null == x && null == y) return 0;
if (null == x) return -1;
if (null == y) return 1;
if (x is string && y is string)
return Compare((string)x, (string)y, zeroesFirst);
return Comparer.Default.Compare(x, y);
}
public static int Compare(string s1, string s2)
{
return Compare(s1, s2, false);
}
public static int Compare(string s1, string s2, bool zeroesFirst)
{
//get rid of special cases
if ((s1 == null) && (s2 == null)) return 0;
else if (s1 == null) return -1;
else if (s2 == null) return 1;
if ((s1.Length <= 0) && (s2.Length <= 0)) return 0;
else if (s1.Length <= 0) return -1;
else if (s2.Length <= 0) return 1;
//special case
bool sp1 = Char.IsLetterOrDigit(s1[0]);
bool sp2 = Char.IsLetterOrDigit(s2[0]);
if (sp1 && !sp2) return 1;
if (!sp1 && sp2) return -1;
int i1 = 0, i2 = 0; //current index
int r = 0; // temp result
char c1, c2;
bool letter1, letter2;
while (true)
{
c1 = s1[i1];
c2 = s2[i2];
sp1 = Char.IsDigit(c1);
sp2 = Char.IsDigit(c2);
if (!sp1 && !sp2)
{
letter1 = Char.IsLetter(c1);
letter2 = Char.IsLetter(c2);
if (letter1 && letter2)
{
r = Char.ToUpper(c1).ToString().CompareTo(Char.ToUpper(c2).ToString());
if (r != 0) return r;
}
else if (!letter1 && !letter2)
{
r = c1.CompareTo(c2);
if (r != 0) return r;
}
else if (!letter1 && letter2)
{
return -1;
}
else if (letter1 && !letter2)
{
return 1;
}
}
else if (sp1 && sp2)
{
r = CompareNum(s1, ref i1, s2, ref i2, zeroesFirst);
if (r != 0) return r;
}
else if (sp1)
{
return -1;
}
else if (sp2)
{
return 1;
}
i1++;
i2++;
if ((i1 >= s1.Length) && (i2 >= s2.Length))
{
return 0;
}
else if (i1 >= s1.Length)
{
return -1;
}
else if (i2 >= s2.Length)
{
return 1;
}
}
}
private static int CompareNum(string s1, ref int i1, string s2, ref int i2, bool zeroesFirst)
{
int nzStart1 = i1, nzStart2 = i2; // nz = non zero
int end1 = i1, end2 = i2;
ScanNumEnd(s1, i1, ref end1, ref nzStart1);
ScanNumEnd(s2, i2, ref end2, ref nzStart2);
int start1 = i1; i1 = end1 - 1;
int start2 = i2; i2 = end2 - 1;
if (zeroesFirst)
{
int zl1 = nzStart1 - start1;
int zl2 = nzStart2 - start2;
if (zl1 > zl2) return -1;
if (zl1 < zl2) return 1;
}
int nzLength1 = end1 - nzStart1;
int nzLength2 = end2 - nzStart2;
if (nzLength1 < nzLength2) return -1;
else if (nzLength1 > nzLength2) return 1;
for (int j1 = nzStart1, j2 = nzStart2; j1 <= i1; j1++, j2++)
{
int r = s1[j1].CompareTo(s2[j2]);
if (r != 0) return r;
}
// the nz parts are equal
int length1 = end1 - start1;
int length2 = end2 - start2;
if (length1 == length2) return 0;
if (length1 > length2) return -1;
return 1;
}
//lookahead
private static void ScanNumEnd(string s, int start, ref int end, ref int nzStart)
{
nzStart = start;
end = start;
bool countZeros = true;
while (Char.IsDigit(s, end))
{
if (countZeros && s[end].Equals('0'))
{
nzStart++;
}
else countZeros = false;
end++;
if (end >= s.Length) break;
}
}
}//EOC
}