Improve LibraryManager.Sort when using "Random" order (#12165)

Co-authored-by: Cody Robibero <cody@robibe.ro>
This commit is contained in:
ItsAllAboutTheCode 2024-07-15 07:44:06 -05:00 committed by GitHub
parent 90dcd9f267
commit 5e840c1db6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 10 deletions

View File

@ -185,6 +185,7 @@
- [Vedant](https://github.com/viktory36/) - [Vedant](https://github.com/viktory36/)
- [NotSaifA](https://github.com/NotSaifA) - [NotSaifA](https://github.com/NotSaifA)
- [HonestlyWhoKnows](https://github.com/honestlywhoknows) - [HonestlyWhoKnows](https://github.com/honestlywhoknows)
- [ItsAllAboutTheCode](https://github.com/ItsAllAboutTheCode)
# Emby Contributors # Emby Contributors

View File

@ -1,4 +1,5 @@
#pragma warning disable CS1591 #pragma warning disable CS1591
#pragma warning disable CA5394
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
@ -16,6 +17,7 @@ using Emby.Server.Implementations.Library.Resolvers;
using Emby.Server.Implementations.Library.Validators; using Emby.Server.Implementations.Library.Validators;
using Emby.Server.Implementations.Playlists; using Emby.Server.Implementations.Playlists;
using Emby.Server.Implementations.ScheduledTasks.Tasks; using Emby.Server.Implementations.ScheduledTasks.Tasks;
using Emby.Server.Implementations.Sorting;
using Jellyfin.Data.Entities; using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums; using Jellyfin.Data.Enums;
using Jellyfin.Extensions; using Jellyfin.Extensions;
@ -1710,13 +1712,19 @@ namespace Emby.Server.Implementations.Library
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<BaseItem> Sort(IEnumerable<BaseItem> items, User? user, IEnumerable<ItemSortBy> sortBy, SortOrder sortOrder) public IEnumerable<BaseItem> Sort(IEnumerable<BaseItem> items, User? user, IEnumerable<ItemSortBy> sortBy, SortOrder sortOrder)
{ {
var isFirst = true;
IOrderedEnumerable<BaseItem>? orderedItems = null; IOrderedEnumerable<BaseItem>? orderedItems = null;
foreach (var orderBy in sortBy.Select(o => GetComparer(o, user)).Where(c => c is not null)) foreach (var orderBy in sortBy.Select(o => GetComparer(o, user)).Where(c => c is not null))
{ {
if (isFirst) if (orderBy is RandomComparer)
{
var randomItems = items.ToArray();
Random.Shared.Shuffle(randomItems);
items = randomItems;
// Items are no longer ordered at this point, so set orderedItems back to null
orderedItems = null;
}
else if (orderedItems is null)
{ {
orderedItems = sortOrder == SortOrder.Descending orderedItems = sortOrder == SortOrder.Descending
? items.OrderByDescending(i => i, orderBy) ? items.OrderByDescending(i => i, orderBy)
@ -1728,8 +1736,6 @@ namespace Emby.Server.Implementations.Library
? orderedItems!.ThenByDescending(i => i, orderBy) ? orderedItems!.ThenByDescending(i => i, orderBy)
: orderedItems!.ThenBy(i => i, orderBy); // orderedItems is set during the first iteration : orderedItems!.ThenBy(i => i, orderBy); // orderedItems is set during the first iteration
} }
isFirst = false;
} }
return orderedItems ?? items; return orderedItems ?? items;
@ -1738,8 +1744,6 @@ namespace Emby.Server.Implementations.Library
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<BaseItem> Sort(IEnumerable<BaseItem> items, User? user, IEnumerable<(ItemSortBy OrderBy, SortOrder SortOrder)> orderBy) public IEnumerable<BaseItem> Sort(IEnumerable<BaseItem> items, User? user, IEnumerable<(ItemSortBy OrderBy, SortOrder SortOrder)> orderBy)
{ {
var isFirst = true;
IOrderedEnumerable<BaseItem>? orderedItems = null; IOrderedEnumerable<BaseItem>? orderedItems = null;
foreach (var (name, sortOrder) in orderBy) foreach (var (name, sortOrder) in orderBy)
@ -1750,7 +1754,15 @@ namespace Emby.Server.Implementations.Library
continue; continue;
} }
if (isFirst) if (comparer is RandomComparer)
{
var randomItems = items.ToArray();
Random.Shared.Shuffle(randomItems);
items = randomItems;
// Items are no longer ordered at this point, so set orderedItems back to null
orderedItems = null;
}
else if (orderedItems is null)
{ {
orderedItems = sortOrder == SortOrder.Descending orderedItems = sortOrder == SortOrder.Descending
? items.OrderByDescending(i => i, comparer) ? items.OrderByDescending(i => i, comparer)
@ -1762,8 +1774,6 @@ namespace Emby.Server.Implementations.Library
? orderedItems!.ThenByDescending(i => i, comparer) ? orderedItems!.ThenByDescending(i => i, comparer)
: orderedItems!.ThenBy(i => i, comparer); // orderedItems is set during the first iteration : orderedItems!.ThenBy(i => i, comparer); // orderedItems is set during the first iteration
} }
isFirst = false;
} }
return orderedItems ?? items; return orderedItems ?? items;