diff --git a/Jellyfin.Api/Helpers/RequestHelpers.cs b/Jellyfin.Api/Helpers/RequestHelpers.cs
index 056ad83daf..cd1e31e804 100644
--- a/Jellyfin.Api/Helpers/RequestHelpers.cs
+++ b/Jellyfin.Api/Helpers/RequestHelpers.cs
@@ -25,22 +25,27 @@ namespace Jellyfin.Api.Helpers
/// Sort By. Comma delimited string.
/// Sort Order. Comma delimited string.
/// Order By.
- public static ValueTuple[] GetOrderBy(IReadOnlyList sortBy, IReadOnlyList requestedSortOrder)
+ public static (string, SortOrder)[] GetOrderBy(IReadOnlyList sortBy, IReadOnlyList requestedSortOrder)
{
if (sortBy.Count == 0)
{
return Array.Empty>();
}
- var result = new ValueTuple[sortBy.Count];
- for (var i = 0; i < sortBy.Count; i++)
+ var result = new (string, SortOrder)[sortBy.Count];
+ var i = 0;
+ // Add elements which have a SortOrder specified
+ for (; i < requestedSortOrder.Count; i++)
{
- var sortOrderIndex = requestedSortOrder.Count > i ? i : 0;
+ result[i] = (sortBy[i], requestedSortOrder[i]);
+ }
- var sortOrder = requestedSortOrder.Count > sortOrderIndex
- ? requestedSortOrder[sortOrderIndex]
- : SortOrder.Ascending;
- result[i] = new ValueTuple(sortBy[i], sortOrder);
+ // Add remaining elements with the first specified SortOrder
+ // or the default one if no SortOrders are specified
+ var order = requestedSortOrder.Count > 0 ? requestedSortOrder[0] : SortOrder.Ascending;
+ for (; i < sortBy.Count; i++)
+ {
+ result[i] = (sortBy[i], order);
}
return result;
diff --git a/tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs b/tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs
new file mode 100644
index 0000000000..606041c7f4
--- /dev/null
+++ b/tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using Jellyfin.Api.Helpers;
+using Jellyfin.Data.Enums;
+using Xunit;
+
+namespace Jellyfin.Api.Tests.Helpers
+{
+ public class RequestHelpersTests
+ {
+ [Theory]
+ [MemberData(nameof(GetOrderBy_Success_TestData))]
+ public void GetOrderBy_Success(IReadOnlyList sortBy, IReadOnlyList requestedSortOrder, (string, SortOrder)[] expected)
+ {
+ Assert.Equal(expected, RequestHelpers.GetOrderBy(sortBy, requestedSortOrder));
+ }
+
+ public static IEnumerable