OfficialRatingComparer.cs 1.4 KB

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