diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index 8f63bd6b4a..edbc846d63 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -185,6 +185,7 @@
- [Vedant](https://github.com/viktory36/)
- [NotSaifA](https://github.com/NotSaifA)
- [HonestlyWhoKnows](https://github.com/honestlywhoknows)
+ - [ItsAllAboutTheCode](https://github.com/ItsAllAboutTheCode)
# Emby Contributors
diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs
index ac2248264d..cbded1ec6a 100644
--- a/Emby.Server.Implementations/Library/LibraryManager.cs
+++ b/Emby.Server.Implementations/Library/LibraryManager.cs
@@ -1,4 +1,5 @@
#pragma warning disable CS1591
+#pragma warning disable CA5394
using System;
using System.Collections.Concurrent;
@@ -16,6 +17,7 @@ using Emby.Server.Implementations.Library.Resolvers;
using Emby.Server.Implementations.Library.Validators;
using Emby.Server.Implementations.Playlists;
using Emby.Server.Implementations.ScheduledTasks.Tasks;
+using Emby.Server.Implementations.Sorting;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using Jellyfin.Extensions;
@@ -1710,13 +1712,19 @@ namespace Emby.Server.Implementations.Library
///
public IEnumerable Sort(IEnumerable items, User? user, IEnumerable sortBy, SortOrder sortOrder)
{
- var isFirst = true;
-
IOrderedEnumerable? orderedItems = 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
? items.OrderByDescending(i => i, orderBy)
@@ -1728,8 +1736,6 @@ namespace Emby.Server.Implementations.Library
? orderedItems!.ThenByDescending(i => i, orderBy)
: orderedItems!.ThenBy(i => i, orderBy); // orderedItems is set during the first iteration
}
-
- isFirst = false;
}
return orderedItems ?? items;
@@ -1738,8 +1744,6 @@ namespace Emby.Server.Implementations.Library
///
public IEnumerable Sort(IEnumerable items, User? user, IEnumerable<(ItemSortBy OrderBy, SortOrder SortOrder)> orderBy)
{
- var isFirst = true;
-
IOrderedEnumerable? orderedItems = null;
foreach (var (name, sortOrder) in orderBy)
@@ -1750,7 +1754,15 @@ namespace Emby.Server.Implementations.Library
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
? items.OrderByDescending(i => i, comparer)
@@ -1762,8 +1774,6 @@ namespace Emby.Server.Implementations.Library
? orderedItems!.ThenByDescending(i => i, comparer)
: orderedItems!.ThenBy(i => i, comparer); // orderedItems is set during the first iteration
}
-
- isFirst = false;
}
return orderedItems ?? items;