diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index b134dfde20..cda4c31ad8 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -807,52 +807,27 @@ namespace MediaBrowser.Controller.Entities return false; } - private class OneTimeQueue - { - private List items; - private int start; - public OneTimeQueue(int capacity) - { - items = new List(capacity); - start = 0; - } - public OneTimeQueue(int capacity, T first) - { - items = new List(capacity); - items.Add(first); - start = 0; - } - public void Enqueue(T item) - { - items.Add(item); - } - public T Dequeue() - { - start++; - return items[start - 1]; - } - } - private IReadOnlyList SortItemsByRequest(InternalItemsQuery query, IReadOnlyList items) { var ids = query.ItemIds; - var positions = new Dictionary>(ids.Length); + int size = items.Count; + + // ids can potentially contain non-unique guids, but query result cannot, + // so we include only first occurrence of each guid + var positions = new Dictionary(size); + int index = 0; for (int i = 0; i < ids.Length; i++) { - if (positions.TryGetValue(ids[i], out var q)) - { - q.Enqueue(i); - } - else + if (positions.TryAdd(ids[i], index)) { - positions.Add(ids[i], new OneTimeQueue(4 /* wild guess */, i)); + index++; } } - var newItems = new BaseItem[ids.Count]; + var newItems = new BaseItem[size]; foreach(var item in items) { - newItems[positions[item.Id].Dequeue()] = item; + newItems[positions[item.Id]] = item; } return newItems; }