Only sort item by width when they have the same path (#12626)

This commit is contained in:
gnattu 2024-09-18 21:22:53 +08:00 committed by GitHub
parent 901573473d
commit 97d2f778f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 6 deletions

View file

@ -1087,12 +1087,7 @@ namespace MediaBrowser.Controller.Entities
return 1;
}).ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0)
.ThenByDescending(i =>
{
var stream = i.VideoStream;
return stream is null || stream.Width is null ? 0 : stream.Width.Value;
})
.ThenByDescending(i => i, new MediaSourceWidthComparator())
.ToList();
}

View file

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Runtime.Intrinsics.X86;
using MediaBrowser.Model.Dto;
namespace MediaBrowser.Controller.Entities;
/// <summary>
/// Compare MediaSource of the same file by Video width <see cref="IComparer{T}" />.
/// </summary>
public class MediaSourceWidthComparator : IComparer<MediaSourceInfo>
{
/// <inheritdoc />
public int Compare(MediaSourceInfo? x, MediaSourceInfo? y)
{
if (x is null && y is null)
{
return 0;
}
if (x is null)
{
return -1;
}
if (y is null)
{
return 1;
}
if (string.Equals(x.Path, y.Path, StringComparison.OrdinalIgnoreCase))
{
if (x.VideoStream is null && y.VideoStream is null)
{
return 0;
}
if (x.VideoStream is null)
{
return -1;
}
if (y.VideoStream is null)
{
return 1;
}
var xWidth = x.VideoStream.Width ?? 0;
var yWidth = y.VideoStream.Width ?? 0;
return xWidth - yWidth;
}
return 0;
}
}