@@ -16,10 +16,21 @@ public class PagedCollection<T> : IPagedCollection, INotifyPropertyChanged
1616 private IReadOnlyList < T > _filtered = [ ] ;
1717
1818 private readonly List < Func < T , bool > > _filters = [ ] ;
19+ private readonly List < ( Func < T , object > selector , bool ascending ) > _sorts = [ ] ;
1920 private Func < T , string > ? _searchSelector ;
2021 private string ? _searchTerm ;
2122 private bool _useWildcards ;
2223
24+ /// <summary>
25+ /// Gets a value indicating whether the collection is sorted.
26+ /// </summary>
27+ public bool IsSorted => _sorts . Count > 0 ;
28+
29+ /// <summary>
30+ /// Occurs when the sorting changes.
31+ /// </summary>
32+ public event EventHandler ? SortChanged ;
33+
2334 /// <summary>
2435 /// Initializes a new instance of the <see cref="PagedCollection{T}"/> class.
2536 /// </summary>
@@ -73,6 +84,30 @@ public void SetSearch(Func<T, string> selector, string? term, bool useWildcards
7384 ApplyFiltering ( ) ;
7485 }
7586
87+ /// <summary>
88+ /// Sets the sort criteria for the collection.
89+ /// </summary>
90+ /// <typeparam name="TKey">The type of the key to sort by.</typeparam>
91+ /// <param name="selector">A function to extract the key for sorting.</param>
92+ /// <param name="ascending">A value indicating whether to sort in ascending order.</param>
93+ public void SetSort < TKey > ( Func < T , TKey > selector , bool ascending )
94+ {
95+ _sorts . Clear ( ) ;
96+ _sorts . Add ( ( x => selector ( x ) ! , ascending ) ) ;
97+ ApplyFiltering ( ) ;
98+ SortChanged ? . Invoke ( this , EventArgs . Empty ) ;
99+ }
100+
101+ /// <summary>
102+ /// Clears the sort criteria from the collection.
103+ /// </summary>
104+ public void ClearSort ( )
105+ {
106+ _sorts . Clear ( ) ;
107+ ApplyFiltering ( ) ;
108+ SortChanged ? . Invoke ( this , EventArgs . Empty ) ;
109+ }
110+
76111 private void ApplyFiltering ( )
77112 {
78113 IEnumerable < T > query = _source ;
@@ -97,6 +132,23 @@ private void ApplyFiltering()
97132 }
98133 }
99134
135+ if ( _sorts . Count > 0 )
136+ {
137+ IOrderedEnumerable < T > ? orderedQuery = null ;
138+ foreach ( var ( selector , ascending ) in _sorts )
139+ {
140+ if ( orderedQuery == null )
141+ {
142+ orderedQuery = ascending ? query . OrderBy ( selector ) : query . OrderByDescending ( selector ) ;
143+ }
144+ else
145+ {
146+ orderedQuery = ascending ? orderedQuery . ThenBy ( selector ) : orderedQuery . ThenByDescending ( selector ) ;
147+ }
148+ }
149+ query = orderedQuery ?? query ;
150+ }
151+
100152 _filtered = [ .. query ] ;
101153 _currentPage = 0 ;
102154 RaiseAllChanged ( ) ;
0 commit comments