forked from c0ldw1nter/echoes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortableBindingList.cs
130 lines (114 loc) · 4.05 KB
/
SortableBindingList.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Echoes
{
public class SortableBindingList<T> : BindingList<T> where T : class
{
private bool _isSorted;
private ListSortDirection _sortDirection = ListSortDirection.Ascending;
private PropertyDescriptor _sortProperty;
/// <summary>
/// Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
/// </summary>
///
public SortableBindingList()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
/// </summary>
/// <param name="list">An <see cref="T:System.Collections.Generic.IList`1" /> of items to be contained in the <see cref="T:System.ComponentModel.BindingList`1" />.</param>
public SortableBindingList(IList<T> list)
: base(list)
{
}
/// <summary>
/// Gets a value indicating whether the list supports sorting.
/// </summary>
protected override bool SupportsSortingCore
{
get { return true; }
}
/// <summary>
/// Gets a value indicating whether the list is sorted.
/// </summary>
protected override bool IsSortedCore
{
get { return _isSorted; }
}
/// <summary>
/// Gets the direction the list is sorted.
/// </summary>
protected override ListSortDirection SortDirectionCore
{
get { return _sortDirection; }
}
/// <summary>
/// Gets the property descriptor that is used for sorting the list if sorting is implemented in a derived class; otherwise, returns null
/// </summary>
protected override PropertyDescriptor SortPropertyCore
{
get { return _sortProperty; }
}
/// <summary>
/// Removes any sort applied with ApplySortCore if sorting is implemented
/// </summary>
protected override void RemoveSortCore()
{
_sortDirection = ListSortDirection.Ascending;
_sortProperty = null;
_isSorted = false; //thanks Luca
}
/// <summary>
/// Sorts the items if overridden in a derived class
/// </summary>
/// <param name="prop"></param>
/// <param name="direction"></param>
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
_sortProperty = prop;
_sortDirection = direction;
List<T> list = Items as List<T>;
if (list == null) return;
list.Sort(Compare);
_isSorted = true;
//fire an event that the list has been changed.
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
private int Compare(T lhs, T rhs)
{
var result = OnComparison(lhs, rhs);
//invert if descending
if (_sortDirection == ListSortDirection.Descending)
result = -result;
return result;
}
private int OnComparison(T lhs, T rhs)
{
object lhsValue = lhs == null ? null : _sortProperty.GetValue(lhs);
object rhsValue = rhs == null ? null : _sortProperty.GetValue(rhs);
if (lhsValue == null)
{
return (rhsValue == null) ? 0 : -1; //nulls are equal
}
if (rhsValue == null)
{
return 1; //first has value, second doesn't
}
if (lhsValue is IComparable)
{
return ((IComparable)lhsValue).CompareTo(rhsValue);
}
if (lhsValue.Equals(rhsValue))
{
return 0; //both are the same
}
//not comparable, compare ToString
return lhsValue.ToString().CompareTo(rhsValue.ToString());
}
}
}