Skip to content

Commit 13fdad7

Browse files
committed
Add SortDescription for object comparer
#881
1 parent ffc8a92 commit 13fdad7

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

Microsoft.Toolkit.Uwp.UI/AdvancedCollectionView/AdvancedCollectionView.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,15 +368,29 @@ int IComparer<object>.Compare(object x, object y)
368368
var typeInfo = x.GetType().GetTypeInfo();
369369
foreach (var sd in _sortDescriptions)
370370
{
371-
_sortProperties[sd.PropertyName] = typeInfo.GetDeclaredProperty(sd.PropertyName);
371+
if (!string.IsNullOrEmpty(sd.PropertyName))
372+
{
373+
_sortProperties[sd.PropertyName] = typeInfo.GetDeclaredProperty(sd.PropertyName);
374+
}
372375
}
373376
}
374377

375378
foreach (var sd in _sortDescriptions)
376379
{
377-
var pi = _sortProperties[sd.PropertyName];
378-
var cx = pi.GetValue(x) as IComparable;
379-
var cy = pi.GetValue(y) as IComparable;
380+
IComparable cx, cy;
381+
382+
if (string.IsNullOrEmpty(sd.PropertyName))
383+
{
384+
cx = x as IComparable;
385+
cy = y as IComparable;
386+
}
387+
else
388+
{
389+
var pi = _sortProperties[sd.PropertyName];
390+
cx = pi.GetValue(x) as IComparable;
391+
cy = pi.GetValue(y) as IComparable;
392+
}
393+
380394
try
381395
{
382396
// ReSharper disable once PossibleUnintendedReferenceComparison

Microsoft.Toolkit.Uwp.UI/AdvancedCollectionView/SortDescription.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ public class SortDescription
2020
/// <summary>
2121
/// Gets the name of property to sort on
2222
/// </summary>
23-
public string PropertyName { get; private set; }
23+
public string PropertyName { get; }
2424

2525
/// <summary>
2626
/// Gets the direction of sort
2727
/// </summary>
28-
public SortDirection Direction { get; private set; }
28+
public SortDirection Direction { get; }
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="SortDescription"/> class that describes
32+
/// a sort on the object itself
33+
/// </summary>
34+
/// <param name="direction">direction of sort</param>
35+
public SortDescription(SortDirection direction)
36+
{
37+
Direction = direction;
38+
}
2939

3040
/// <summary>
3141
/// Initializes a new instance of the <see cref="SortDescription"/> class.

UnitTests/UI/Person.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,29 @@
1010
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
1111
// ******************************************************************
1212

13+
using System;
14+
1315
namespace UnitTests.UI
1416
{
1517
/// <summary>
1618
/// Sample class to test AdvancedCollectionViewSource functionality
1719
/// </summary>
18-
internal class Person
20+
internal class Person : IComparable
1921
{
2022
public string Name { get; set; }
2123

2224
public int Age { get; set; }
25+
26+
public int CompareTo(object obj)
27+
{
28+
var other = obj as Person;
29+
30+
if (other == null)
31+
{
32+
return -1;
33+
}
34+
35+
return Age.CompareTo(other.Age);
36+
}
2337
}
2438
}

UnitTests/UI/Test_AdvancedCollectionView.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,54 @@ public void Test_AdvancedCollectionView_Combined()
180180
Assert.AreEqual(((Person)a.First()).Age, 42);
181181
Assert.AreEqual(a.Count, 2);
182182
}
183+
184+
[TestCategory("AdvancedCollectionView")]
185+
[UITestMethod]
186+
public void Test_AdvancedCollectionView_Sorting_OnSelf()
187+
{
188+
var l = new ObservableCollection<Person>
189+
{
190+
new Person()
191+
{
192+
Name = "lorem",
193+
Age = 4
194+
},
195+
new Person()
196+
{
197+
Name = "imsum",
198+
Age = 8
199+
},
200+
new Person()
201+
{
202+
Name = "dolor",
203+
Age = 15
204+
},
205+
new Person()
206+
{
207+
Name = "sit",
208+
Age = 16
209+
},
210+
new Person()
211+
{
212+
Name = "amet",
213+
Age = 23
214+
},
215+
new Person()
216+
{
217+
Name = "consectetur",
218+
Age = 42
219+
},
220+
};
221+
222+
var a = new AdvancedCollectionView(l)
223+
{
224+
SortDescriptions =
225+
{
226+
new SortDescription(SortDirection.Descending)
227+
}
228+
};
229+
230+
Assert.AreEqual(((Person)a.First()).Age, 42);
231+
}
183232
}
184233
}

0 commit comments

Comments
 (0)