private static IEnumerable <TSource> OrderByThenTakeImpl <TSource, TKey>(IEnumerable <TSource> source, int count, IHeap <TSource> heap) { foreach (var item in source) { heap.Add(item); if (heap.Count > count) { // Remove minimum/maximum value heap.Remove(); } } return(heap.ConsumeHeap().Reverse()); }