PremiereDateComparer.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 == null)
  30. {
  31. return DateTime.MinValue;
  32. }
  33. if (x.PremiereDate.HasValue)
  34. {
  35. return x.PremiereDate.Value;
  36. }
  37. if (x.ProductionYear.HasValue)
  38. {
  39. try
  40. {
  41. return new DateTime(x.ProductionYear.Value, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  42. }
  43. catch (ArgumentOutOfRangeException)
  44. {
  45. // Don't blow up if the item has a bad ProductionYear, just return MinValue
  46. }
  47. }
  48. return DateTime.MinValue;
  49. }
  50. /// <summary>
  51. /// Gets the name.
  52. /// </summary>
  53. /// <value>The name.</value>
  54. public string Name => ItemSortBy.PremiereDate;
  55. }
  56. }