-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSortingFunctions.cs
347 lines (321 loc) · 15.6 KB
/
SortingFunctions.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using DocumentFormat.OpenXml.Spreadsheet;
namespace SpreadsheetLight
{
public partial class SLDocument
{
/// <summary>
/// Sort data by column.
/// </summary>
/// <param name="StartCellReference">The cell reference of the start cell of the cell range to be sorted, such as "A1". This is typically the top-left cell.</param>
/// <param name="EndCellReference">The cell reference of the end cell of the cell range to be sorted, such as "A1". This is typically the bottom-right cell.</param>
/// <param name="SortByColumnName">The column name of the column to be sorted by, such as "AA".</param>
/// <param name="SortAscending">True to sort in ascending order. False to sort in descending order.</param>
public void Sort(string StartCellReference, string EndCellReference, string SortByColumnName, bool SortAscending)
{
int iStartRowIndex = -1;
int iStartColumnIndex = -1;
int iEndRowIndex = -1;
int iEndColumnIndex = -1;
int iSortByColumnIndex = -1;
if (SLTool.FormatCellReferenceToRowColumnIndex(StartCellReference, out iStartRowIndex, out iStartColumnIndex)
&& SLTool.FormatCellReferenceToRowColumnIndex(EndCellReference, out iEndRowIndex, out iEndColumnIndex))
{
iSortByColumnIndex = SLTool.ToColumnIndex(SortByColumnName);
this.Sort(iStartRowIndex, iStartColumnIndex, iEndRowIndex, iEndColumnIndex, true, iSortByColumnIndex, SortAscending);
}
}
/// <summary>
/// Sort data by row.
/// </summary>
/// <param name="StartCellReference">The cell reference of the start cell of the cell range to be sorted, such as "A1". This is typically the top-left cell.</param>
/// <param name="EndCellReference">The cell reference of the end cell of the cell range to be sorted, such as "A1". This is typically the bottom-right cell.</param>
/// <param name="SortByRowIndex">The row index of the row to be sorted by.</param>
/// <param name="SortAscending">True to sort in ascending order. False to sort in descending order.</param>
public void Sort(string StartCellReference, string EndCellReference, int SortByRowIndex, bool SortAscending)
{
int iStartRowIndex = -1;
int iStartColumnIndex = -1;
int iEndRowIndex = -1;
int iEndColumnIndex = -1;
if (SLTool.FormatCellReferenceToRowColumnIndex(StartCellReference, out iStartRowIndex, out iStartColumnIndex)
&& SLTool.FormatCellReferenceToRowColumnIndex(EndCellReference, out iEndRowIndex, out iEndColumnIndex))
{
this.Sort(iStartRowIndex, iStartColumnIndex, iEndRowIndex, iEndColumnIndex, false, SortByRowIndex, SortAscending);
}
}
/// <summary>
/// Sort data by column.
/// </summary>
/// <param name="StartRowIndex">The row index of the start row. This is typically the top row.</param>
/// <param name="StartColumnIndex">The column index of the start column. This is typically the left-most column.</param>
/// <param name="EndRowIndex">The row index of the end row. This is typically the bottom row.</param>
/// <param name="EndColumnIndex">The column index of the end column. This is typically the right-most column.</param>
/// <param name="SortByColumnIndex">The column index of the column to be sorted by.</param>
/// <param name="SortAscending">True to sort in ascending order. False to sort in descending order.</param>
public void Sort(int StartRowIndex, int StartColumnIndex, int EndRowIndex, int EndColumnIndex, int SortByColumnIndex, bool SortAscending)
{
this.Sort(StartRowIndex, StartColumnIndex, EndRowIndex, EndColumnIndex, true, SortByColumnIndex, SortAscending);
}
/// <summary>
/// Sort data either by column or row.
/// </summary>
/// <param name="StartRowIndex">The row index of the start row. This is typically the top row.</param>
/// <param name="StartColumnIndex">The column index of the start column. This is typically the left-most column.</param>
/// <param name="EndRowIndex">The row index of the end row. This is typically the bottom row.</param>
/// <param name="EndColumnIndex">The column index of the end column. This is typically the right-most column.</param>
/// <param name="SortByColumn">True to sort by column. False to sort by row.</param>
/// <param name="SortByIndex">The row or column index of the row or column to be sorted by, depending on <paramref name="SortByColumn"/></param>
/// <param name="SortAscending">True to sort in ascending order. False to sort in descending order.</param>
public void Sort(int StartRowIndex, int StartColumnIndex, int EndRowIndex, int EndColumnIndex, bool SortByColumn, int SortByIndex, bool SortAscending)
{
int iStartRowIndex = 1, iEndRowIndex = 1, iStartColumnIndex = 1, iEndColumnIndex = 1;
if (StartRowIndex < EndRowIndex)
{
iStartRowIndex = StartRowIndex;
iEndRowIndex = EndRowIndex;
}
else
{
iStartRowIndex = EndRowIndex;
iEndRowIndex = StartRowIndex;
}
if (StartColumnIndex < EndColumnIndex)
{
iStartColumnIndex = StartColumnIndex;
iEndColumnIndex = EndColumnIndex;
}
else
{
iStartColumnIndex = EndColumnIndex;
iEndColumnIndex = StartColumnIndex;
}
if (iStartRowIndex < 1) iStartRowIndex = 1;
if (iStartColumnIndex < 1) iStartColumnIndex = 1;
if (iEndRowIndex > SLConstants.RowLimit) iEndRowIndex = SLConstants.RowLimit;
if (iEndColumnIndex > SLConstants.ColumnLimit) iEndColumnIndex = SLConstants.ColumnLimit;
// if the given index is out of the data range, then don't have to sort.
if (SortByColumn)
{
if (SortByIndex < iStartColumnIndex || SortByIndex > iEndColumnIndex) return;
}
else
{
if (SortByIndex < iStartRowIndex || SortByIndex > iEndRowIndex) return;
}
Dictionary<SLCellPoint, SLCell> datacells = new Dictionary<SLCellPoint, SLCell>();
SLCellPoint pt;
int i, j;
for (i = iStartRowIndex; i <= iEndRowIndex; ++i)
{
for (j = iStartColumnIndex; j <= iEndColumnIndex; ++j)
{
pt = new SLCellPoint(i, j);
if (slws.Cells.ContainsKey(pt))
{
datacells[pt] = slws.Cells[pt].Clone();
slws.Cells.Remove(pt);
}
}
}
List<SLSortItem> listNumbers = new List<SLSortItem>();
List<SLSortItem> listText = new List<SLSortItem>();
List<SLSortItem> listBoolean = new List<SLSortItem>();
List<SLSortItem> listEmpty = new List<SLSortItem>();
bool bValue = false;
double fValue = 0.0;
string sText = string.Empty;
SLRstType rst;
int index = 0;
int iStartIndex = -1;
int iEndIndex = -1;
if (SortByColumn)
{
iStartIndex = iStartRowIndex;
iEndIndex = iEndRowIndex;
}
else
{
iStartIndex = iStartColumnIndex;
iEndIndex = iEndColumnIndex;
}
for (i = iStartIndex; i <= iEndIndex; ++i)
{
if (SortByColumn) pt = new SLCellPoint(i, SortByIndex);
else pt = new SLCellPoint(SortByIndex, i);
if (datacells.ContainsKey(pt))
{
if (datacells[pt].DataType == CellValues.Number)
{
if (datacells[pt].CellText != null)
{
if (double.TryParse(datacells[pt].CellText, out fValue))
{
listNumbers.Add(new SLSortItem() { Number = fValue, Index = i });
}
else
{
listText.Add(new SLSortItem() { Text = datacells[pt].CellText, Index = i });
}
}
else
{
listNumbers.Add(new SLSortItem() { Number = datacells[pt].NumericValue, Index = i });
}
}
else if (datacells[pt].DataType == CellValues.SharedString)
{
index = -1;
if (datacells[pt].CellText != null)
{
if (int.TryParse(datacells[pt].CellText, out index)
&& index >= 0 && index < listSharedString.Count)
{
rst = new SLRstType(SLConstants.OfficeThemeMajorLatinFont, SLConstants.OfficeThemeMinorLatinFont, new List<System.Drawing.Color>(), new List<System.Drawing.Color>());
rst.FromSharedStringItem(new SharedStringItem() { InnerXml = listSharedString[index] });
listText.Add(new SLSortItem() { Text = rst.ToPlainString(), Index = i });
}
else
{
listText.Add(new SLSortItem() { Text = datacells[pt].CellText, Index = i });
}
}
else
{
index = Convert.ToInt32(datacells[pt].NumericValue);
if (index >= 0 && index < listSharedString.Count)
{
rst = new SLRstType(SLConstants.OfficeThemeMajorLatinFont, SLConstants.OfficeThemeMinorLatinFont, new List<System.Drawing.Color>(), new List<System.Drawing.Color>());
rst.FromSharedStringItem(new SharedStringItem() { InnerXml = listSharedString[index] });
listText.Add(new SLSortItem() { Text = rst.ToPlainString(), Index = i });
}
else
{
listText.Add(new SLSortItem() { Text = datacells[pt].NumericValue.ToString(CultureInfo.InvariantCulture), Index = i });
}
}
}
else if (datacells[pt].DataType == CellValues.Boolean)
{
if (datacells[pt].CellText != null)
{
if (double.TryParse(datacells[pt].CellText, NumberStyles.Any, CultureInfo.InvariantCulture, out fValue))
{
listBoolean.Add(new SLSortItem() { Number = fValue > 0.5 ? 1.0 : 0.0, Index = i });
}
else if (bool.TryParse(datacells[pt].CellText, out bValue))
{
listBoolean.Add(new SLSortItem() { Number = bValue ? 1.0 : 0.0, Index = i });
}
else
{
listText.Add(new SLSortItem() { Text = datacells[pt].CellText, Index = i });
}
}
else
{
listBoolean.Add(new SLSortItem() { Number = datacells[pt].NumericValue > 0.5 ? 1.0 : 0.0, Index = i });
}
}
else
{
listText.Add(new SLSortItem() { Text = datacells[pt].CellText, Index = i });
}
}
else
{
listEmpty.Add(new SLSortItem() { Index = i });
}
}
listNumbers.Sort(new SLSortItemNumberComparer());
if (!SortAscending) listNumbers.Reverse();
listText.Sort(new SLSortItemTextComparer());
if (!SortAscending) listText.Reverse();
listBoolean.Sort(new SLSortItemNumberComparer());
if (!SortAscending) listBoolean.Reverse();
Dictionary<int, int> ReverseIndex = new Dictionary<int,int>();
if (SortAscending)
{
j = iStartIndex;
for (i = 0; i < listNumbers.Count; ++i)
{
ReverseIndex[listNumbers[i].Index] = j;
++j;
}
for (i = 0; i < listText.Count; ++i)
{
ReverseIndex[listText[i].Index] = j;
++j;
}
for (i = 0; i < listBoolean.Count; ++i)
{
ReverseIndex[listBoolean[i].Index] = j;
++j;
}
for (i = 0; i < listEmpty.Count; ++i)
{
ReverseIndex[listEmpty[i].Index] = j;
++j;
}
}
else
{
j = iStartIndex;
for (i = 0; i < listBoolean.Count; ++i)
{
ReverseIndex[listBoolean[i].Index] = j;
++j;
}
for (i = 0; i < listText.Count; ++i)
{
ReverseIndex[listText[i].Index] = j;
++j;
}
for (i = 0; i < listNumbers.Count; ++i)
{
ReverseIndex[listNumbers[i].Index] = j;
++j;
}
for (i = 0; i < listEmpty.Count; ++i)
{
ReverseIndex[listEmpty[i].Index] = j;
++j;
}
}
List<SLCellPoint> listCellKeys = datacells.Keys.ToList<SLCellPoint>();
SLCellPoint newpt;
for (i = 0; i < listCellKeys.Count; ++i)
{
pt = listCellKeys[i];
if (SortByColumn)
{
if (ReverseIndex.ContainsKey(pt.RowIndex))
{
newpt = new SLCellPoint(ReverseIndex[pt.RowIndex], pt.ColumnIndex);
}
else
{
// shouldn't happen, but just in case...
newpt = new SLCellPoint(pt.RowIndex, pt.ColumnIndex);
}
}
else
{
if (ReverseIndex.ContainsKey(pt.ColumnIndex))
{
newpt = new SLCellPoint(pt.RowIndex, ReverseIndex[pt.ColumnIndex]);
}
else
{
// shouldn't happen, but just in case...
newpt = new SLCellPoint(pt.RowIndex, pt.ColumnIndex);
}
}
slws.Cells[newpt] = datacells[pt];
}
}
}
}