AlbumArtistComparer.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Linq;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Sorting;
  5. using MediaBrowser.Model.Querying;
  6. using System;
  7. namespace MediaBrowser.Server.Implementations.Sorting
  8. {
  9. /// <summary>
  10. /// Class AlbumArtistComparer
  11. /// </summary>
  12. public class AlbumArtistComparer : IBaseItemComparer
  13. {
  14. /// <summary>
  15. /// Compares the specified x.
  16. /// </summary>
  17. /// <param name="x">The x.</param>
  18. /// <param name="y">The y.</param>
  19. /// <returns>System.Int32.</returns>
  20. public int Compare(BaseItem x, BaseItem y)
  21. {
  22. return string.Compare(GetValue(x), GetValue(y), StringComparison.CurrentCultureIgnoreCase);
  23. }
  24. /// <summary>
  25. /// Gets the value.
  26. /// </summary>
  27. /// <param name="x">The x.</param>
  28. /// <returns>System.String.</returns>
  29. private string GetValue(BaseItem x)
  30. {
  31. var audio = x as Audio;
  32. if (audio != null)
  33. {
  34. return audio.AlbumArtist;
  35. }
  36. var album = x as MusicAlbum;
  37. if (album != null)
  38. {
  39. var song = album.RecursiveChildren
  40. .OfType<Audio>()
  41. .FirstOrDefault(i => !string.IsNullOrEmpty(i.AlbumArtist));
  42. if (song != null)
  43. {
  44. return song.AlbumArtist;
  45. }
  46. }
  47. return null;
  48. }
  49. /// <summary>
  50. /// Gets the name.
  51. /// </summary>
  52. /// <value>The name.</value>
  53. public string Name
  54. {
  55. get { return ItemSortBy.AlbumArtist; }
  56. }
  57. }
  58. }