|
| 1 | +using System.Collections; |
1 | 2 | using System.Collections.ObjectModel; |
2 | 3 | using System.ComponentModel; |
3 | 4 | using System.Net.Http; |
@@ -489,19 +490,34 @@ public void ClearSelection() |
489 | 490 | public void SetSortDirection(bool ascending) => _ascending = ascending; |
490 | 491 |
|
491 | 492 | /// <summary>Returns <paramref name="items"/> in the current sort order.</summary> |
492 | | - public IEnumerable<PackageWrapper> ApplyToList(IEnumerable<PackageWrapper> items) => |
493 | | - _ascending |
494 | | - ? items.OrderBy(GetSortKey, StringComparer.OrdinalIgnoreCase) |
495 | | - : items.OrderByDescending(GetSortKey, StringComparer.OrdinalIgnoreCase); |
| 493 | + public IEnumerable<PackageWrapper> ApplyToList(IEnumerable<PackageWrapper> items) |
| 494 | + { |
| 495 | + var comparer = Comparer<PackageWrapper>.Create((a, b) => Compare(a, b, CurrentSorter)); |
| 496 | + return _ascending ? items.OrderBy(w => w, comparer) : items.OrderByDescending(w => w, comparer); |
| 497 | + } |
| 498 | + |
| 499 | + // Shared by the "Order by" menu (ApplyToList) and the DataGrid's native column sorting |
| 500 | + // (GetColumnComparer) so both order identically. Versions compare semantically via |
| 501 | + // CoreTools.Version; a string key can't be used there because that struct has no ToString |
| 502 | + // override, so its key would be a constant type name that never sorts. |
| 503 | + private static int Compare(PackageWrapper a, PackageWrapper b, Sorter sorter) => sorter switch |
| 504 | + { |
| 505 | + Sorter.Version => a.Package.NormalizedVersion.CompareTo(b.Package.NormalizedVersion), |
| 506 | + Sorter.NewVersion => a.Package.NormalizedNewVersion.CompareTo(b.Package.NormalizedNewVersion), |
| 507 | + _ => string.Compare(GetSortKey(a, sorter), GetSortKey(b, sorter), StringComparison.OrdinalIgnoreCase), |
| 508 | + }; |
496 | 509 |
|
497 | | - private string GetSortKey(PackageWrapper w) => CurrentSorter switch |
| 510 | + private static string GetSortKey(PackageWrapper w, Sorter sorter) => sorter switch |
498 | 511 | { |
499 | 512 | Sorter.Checked => w.IsChecked ? "0" : "1", |
500 | 513 | Sorter.Name => w.Package.Name, |
501 | 514 | Sorter.Id => w.Package.Id, |
502 | | - Sorter.Version => w.Package.NormalizedVersion.ToString() ?? string.Empty, |
503 | | - Sorter.NewVersion => w.Package.NormalizedNewVersion.ToString() ?? string.Empty, |
504 | 515 | Sorter.Source => w.Package.Source.AsString_DisplayName, |
505 | 516 | _ => w.Package.Name, |
506 | 517 | }; |
| 518 | + |
| 519 | + // Comparer for the DataGrid's native column sorting. Reflection-free (unlike SortMemberPath), |
| 520 | + // so it keeps working under full-trim NativeAOT release builds — see issue #5103. |
| 521 | + public static IComparer GetColumnComparer(Sorter sorter) => |
| 522 | + Comparer<PackageWrapper>.Create((a, b) => Compare(a, b, sorter)); |
507 | 523 | } |
0 commit comments