OfficialRatingComparer.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma warning disable CS1591
  2. using System;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Sorting;
  5. using MediaBrowser.Model.Globalization;
  6. using MediaBrowser.Model.Querying;
  7. namespace Emby.Server.Implementations.Sorting
  8. {
  9. public class OfficialRatingComparer : IBaseItemComparer
  10. {
  11. private readonly ILocalizationManager _localization;
  12. public OfficialRatingComparer(ILocalizationManager localization)
  13. {
  14. _localization = localization;
  15. }
  16. /// <summary>
  17. /// Gets the name.
  18. /// </summary>
  19. /// <value>The name.</value>
  20. public string Name => ItemSortBy.OfficialRating;
  21. /// <summary>
  22. /// Compares the specified x.
  23. /// </summary>
  24. /// <param name="x">The x.</param>
  25. /// <param name="y">The y.</param>
  26. /// <returns>System.Int32.</returns>
  27. public int Compare(BaseItem? x, BaseItem? y)
  28. {
  29. if (x == null)
  30. {
  31. throw new ArgumentNullException(nameof(x));
  32. }
  33. if (y == null)
  34. {
  35. throw new ArgumentNullException(nameof(y));
  36. }
  37. var levelX = string.IsNullOrEmpty(x.OfficialRating) ? 0 : _localization.GetRatingLevel(x.OfficialRating) ?? 0;
  38. var levelY = string.IsNullOrEmpty(y.OfficialRating) ? 0 : _localization.GetRatingLevel(y.OfficialRating) ?? 0;
  39. return levelX.CompareTo(levelY);
  40. }
  41. }
  42. }