I have a binary file which contains more than 100 millions of objects and I read the file using BinaryReader
and return (Yield
) the object (File reader and IEnumerable
implementation is here: Performance comparison of IEnumerable and raising event for each item in source? )
One of object's properties indicates the object rank (like A5
). Assume that I want to get sorted top n
objects based on the property.
I saw the code for OrderBy
function: it uses QuickSort algorithm. I tried to sort the IEnumerable
result with OrderBy
and Take(n)
function together, but I got OutOfMemory
exception, because OrderBy
function creates an array with size of total objects count to implement Quicksort.
Actually, the total memory I need is n so there is no need to create a big array. For instance, if I get Take(1000) it will return only 1000 objects and it doesn't depend on the total count of whole objects.
How can I get the result of OrderBy
function with Take
function? In another word, I need a limited or blocked sorted list with the capacity which is defined by end-user.