-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathListViewItemComparer.cs
More file actions
56 lines (36 loc) · 1.47 KB
/
ListViewItemComparer.cs
File metadata and controls
56 lines (36 loc) · 1.47 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
using Gsemac.Collections;
using System;
using System.Windows.Forms;
namespace Gsemac.Forms {
public class ListViewItemComparer :
ListViewItemComparerBase {
// Public members
public int Column { get; set; }
public SortOrder SortOrder { get; set; }
public ListViewItemComparer(int column) :
this(column, SortOrder.Ascending) {
}
public ListViewItemComparer(int column, SortOrder sortOrder) {
SortOrder = sortOrder;
Column = column;
}
public override int Compare(ListViewItem x, ListViewItem y) {
ListViewItem lhs = SortOrder == SortOrder.Descending ? y : x;
ListViewItem rhs = SortOrder == SortOrder.Descending ? x : y;
if (SortOrder == SortOrder.None) {
// Allow the tag to be used to reset the original sort order.
if (lhs.Tag is int lhsTag && rhs.Tag is int rhsTag)
return lhsTag.CompareTo(rhsTag);
return 0;
}
else {
string lhsStr = lhs.SubItems[Column].Text;
string rhsStr = rhs.SubItems[Column].Text;
if (DateTime.TryParse(lhsStr, out DateTime lhsDate) && DateTime.TryParse(rhsStr, out DateTime rhsDate))
return lhsDate.CompareTo(rhsDate);
else
return new NaturalSortComparer().Compare(lhsStr, rhsStr);
}
}
}
}