From 0aaf2f470a53401705b9943b6fd50cfd8d6fe0c2 Mon Sep 17 00:00:00 2001 From: Maxr1998 Date: Thu, 17 Feb 2022 01:35:59 +0100 Subject: [PATCH] Implement Comparers for (Parent)IndexNumber Can be used in item queries to sort by ParentIndexNumber and IndexNumber (used for disc and track numbers for example). --- .../Sorting/IndexNumberComparer.cs | 50 +++++++++++++++++++ .../Sorting/ParentIndexNumberComparer.cs | 50 +++++++++++++++++++ MediaBrowser.Model/Querying/ItemSortBy.cs | 4 ++ 3 files changed, 104 insertions(+) create mode 100644 Emby.Server.Implementations/Sorting/IndexNumberComparer.cs create mode 100644 Emby.Server.Implementations/Sorting/ParentIndexNumberComparer.cs diff --git a/Emby.Server.Implementations/Sorting/IndexNumberComparer.cs b/Emby.Server.Implementations/Sorting/IndexNumberComparer.cs new file mode 100644 index 000000000..e39280a10 --- /dev/null +++ b/Emby.Server.Implementations/Sorting/IndexNumberComparer.cs @@ -0,0 +1,50 @@ +using System; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Sorting; +using MediaBrowser.Model.Querying; + +namespace Emby.Server.Implementations.Sorting +{ + /// + /// Class IndexNumberComparer. + /// + public class IndexNumberComparer : IBaseItemComparer + { + /// + /// Gets the name. + /// + /// The name. + public string Name => ItemSortBy.IndexNumber; + + /// + /// Compares the specified x. + /// + /// The x. + /// The y. + /// System.Int32. + public int Compare(BaseItem? x, BaseItem? y) + { + if (x == null) + { + throw new ArgumentNullException(nameof(x)); + } + + if (y == null) + { + throw new ArgumentNullException(nameof(y)); + } + + if (!x.IndexNumber.HasValue) + { + return -1; + } + + if (!y.IndexNumber.HasValue) + { + return 1; + } + + return x.IndexNumber.Value.CompareTo(y.IndexNumber.Value); + } + } +} diff --git a/Emby.Server.Implementations/Sorting/ParentIndexNumberComparer.cs b/Emby.Server.Implementations/Sorting/ParentIndexNumberComparer.cs new file mode 100644 index 000000000..ffc4e0cad --- /dev/null +++ b/Emby.Server.Implementations/Sorting/ParentIndexNumberComparer.cs @@ -0,0 +1,50 @@ +using System; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Sorting; +using MediaBrowser.Model.Querying; + +namespace Emby.Server.Implementations.Sorting +{ + /// + /// Class ParentIndexNumberComparer. + /// + public class ParentIndexNumberComparer : IBaseItemComparer + { + /// + /// Gets the name. + /// + /// The name. + public string Name => ItemSortBy.ParentIndexNumber; + + /// + /// Compares the specified x. + /// + /// The x. + /// The y. + /// System.Int32. + public int Compare(BaseItem? x, BaseItem? y) + { + if (x == null) + { + throw new ArgumentNullException(nameof(x)); + } + + if (y == null) + { + throw new ArgumentNullException(nameof(y)); + } + + if (!x.ParentIndexNumber.HasValue) + { + return -1; + } + + if (!y.ParentIndexNumber.HasValue) + { + return 1; + } + + return x.ParentIndexNumber.Value.CompareTo(y.ParentIndexNumber.Value); + } + } +} diff --git a/MediaBrowser.Model/Querying/ItemSortBy.cs b/MediaBrowser.Model/Querying/ItemSortBy.cs index 0b846bb96..0a28acf37 100644 --- a/MediaBrowser.Model/Querying/ItemSortBy.cs +++ b/MediaBrowser.Model/Querying/ItemSortBy.cs @@ -102,5 +102,9 @@ namespace MediaBrowser.Model.Querying public const string DateLastContentAdded = "DateLastContentAdded"; public const string SeriesDatePlayed = "SeriesDatePlayed"; + + public const string ParentIndexNumber = "ParentIndexNumber"; + + public const string IndexNumber = "IndexNumber"; } }