Sort columns with null values always at the bottom

This commit is contained in:
Michael Bucari-Tovo 2025-07-28 09:29:17 -06:00
parent ca30fd41c6
commit 9bca84dca4
2 changed files with 34 additions and 8 deletions

View File

@ -211,6 +211,24 @@ namespace LibationUiBase.GridView
_ => null _ => null
}; };
public bool MemberValueIsDefault(string memberName) => memberName switch
{
nameof(Series) => Book.SeriesLink?.Any() is not true,
nameof(SeriesOrder) => string.IsNullOrWhiteSpace(SeriesOrder.OrderString),
nameof(MyRating) => RatingIsDefault(Book.UserDefinedItem.Rating),
nameof(ProductRating) => RatingIsDefault(Book.Rating),
nameof(Authors) => string.IsNullOrWhiteSpace(Authors),
nameof(Narrators) => string.IsNullOrWhiteSpace(Narrators),
nameof(Description) => string.IsNullOrWhiteSpace(Description),
nameof(Category) => string.IsNullOrWhiteSpace(Category),
nameof(Misc) => string.IsNullOrWhiteSpace(Misc),
nameof(BookTags) => string.IsNullOrWhiteSpace(BookTags),
_ => false
};
private static bool RatingIsDefault(Rating rating)
=> rating is null || (rating.OverallRating == 0 && rating.PerformanceRating == 0 && rating.StoryRating == 0);
public IComparer GetMemberComparer(Type memberType) public IComparer GetMemberComparer(Type memberType)
=> memberTypeComparers.TryGetValue(memberType, out IComparer value) ? value : memberTypeComparers[memberType.BaseType]; => memberTypeComparers.TryGetValue(memberType, out IComparer value) ? value : memberTypeComparers[memberType.BaseType];
@ -341,7 +359,6 @@ namespace LibationUiBase.GridView
return (await Task.WhenAll(tasks)).SelectMany(a => a).ToList(); return (await Task.WhenAll(tasks)).SelectMany(a => a).ToList();
} }
~GridEntry() ~GridEntry()
{ {
PictureStorage.PictureCached -= PictureStorage_PictureCached; PictureStorage.PictureCached -= PictureStorage_PictureCached;

View File

@ -21,15 +21,24 @@ namespace LibationUiBase.GridView
private int InternalCompare(GridEntry x, GridEntry y) private int InternalCompare(GridEntry x, GridEntry y)
{ {
var val1 = x.GetMemberValue(PropertyName); //Default values (e.g. empty strings) always sort to the end of the list.
var val2 = y.GetMemberValue(PropertyName); var val1IsDefault = x.MemberValueIsDefault(PropertyName);
var val2IsDefault = y.MemberValueIsDefault(PropertyName);
var compare = x.GetMemberComparer(val1.GetType()).Compare(val1, val2); if (val1IsDefault && val2IsDefault) return 0;
else if (val1IsDefault && !val2IsDefault) return GetSortOrder() is ListSortDirection.Ascending ? 1 : -1;
else if (!val1IsDefault && val2IsDefault) return GetSortOrder() is ListSortDirection.Ascending ? -1 : 1;
else
{
var val1 = x.GetMemberValue(PropertyName);
var val2 = y.GetMemberValue(PropertyName);
var compare = x.GetMemberComparer(val1.GetType()).Compare(val1, val2);
return compare == 0 && x.Liberate.IsSeries && y.Liberate.IsSeries return compare == 0 && x.Liberate.IsSeries && y.Liberate.IsSeries
//Both a and b are series parents and compare as equal, so break the tie. //Both a and b are series parents and compare as equal, so break the tie.
? x.AudibleProductId.CompareTo(y.AudibleProductId) ? x.AudibleProductId.CompareTo(y.AudibleProductId)
: compare; : compare;
}
} }
public int Compare(GridEntry? geA, GridEntry? geB) public int Compare(GridEntry? geA, GridEntry? geB)