PremiereDateComparer.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Sorting;
  4. using MediaBrowser.Model.Querying;
  5. namespace Emby.Server.Implementations.Sorting
  6. {
  7. /// <summary>
  8. /// Class PremiereDateComparer
  9. /// </summary>
  10. public class PremiereDateComparer : IBaseItemComparer
  11. {
  12. /// <summary>
  13. /// Compares the specified x.
  14. /// </summary>
  15. /// <param name="x">The x.</param>
  16. /// <param name="y">The y.</param>
  17. /// <returns>System.Int32.</returns>
  18. public int Compare(BaseItem x, BaseItem y)
  19. {
  20. return GetDate(x).CompareTo(GetDate(y));
  21. }
  22. /// <summary>
  23. /// Gets the date.
  24. /// </summary>
  25. /// <param name="x">The x.</param>
  26. /// <returns>DateTime.</returns>
  27. private static DateTime GetDate(BaseItem x)
  28. {
  29. if (x.PremiereDate.HasValue)
  30. {
  31. return x.PremiereDate.Value;
  32. }
  33. if (x.ProductionYear.HasValue)
  34. {
  35. try
  36. {
  37. return new DateTime(x.ProductionYear.Value, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  38. }
  39. catch (ArgumentOutOfRangeException)
  40. {
  41. // Don't blow up if the item has a bad ProductionYear, just return MinValue
  42. }
  43. }
  44. return DateTime.MinValue;
  45. }
  46. /// <summary>
  47. /// Gets the name.
  48. /// </summary>
  49. /// <value>The name.</value>
  50. public string Name => ItemSortBy.PremiereDate;
  51. }
  52. }