Fix library ordering - use ID-based lookup for reliable alphabetical sorting

This commit is contained in:
2026-03-06 18:08:53 -05:00
parent 26791b568b
commit a1c9488c6c
@@ -145,6 +145,11 @@ namespace Emby.Server.Implementations.Library
var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
var orders = user.GetPreferenceValues<Guid>(PreferenceKind.OrderedViews);
// Create a lookup dictionary for efficient and reliable ordering
var sortedIndexLookup = sorted
.Select((item, index) => new { item, index })
.ToDictionary(x => x.item.Id, x => x.index);
return list
.OrderBy(i =>
{
@@ -158,7 +163,7 @@ namespace Emby.Server.Implementations.Library
return index == -1 ? int.MaxValue : index;
})
.ThenBy(sorted.IndexOf)
.ThenBy(i => sortedIndexLookup.TryGetValue(i.Id, out var sortIndex) ? sortIndex : int.MaxValue)
.ThenBy(i => i.SortName)
.ToArray();
}