How to efficiently sort and filter a large iterator
You can use a max-heap or min-heap. If you want to find the top $k$ elements, use a min-heap. Start by inserting the first $k$ elements. For every subsequent element, compare it to the root of the min-heap. If the new element is smaller, continue to the next element. Otherwise, remove the root and insert instead the new element, operations which can be done in $O(\log k)$. In total, this is an $O(n\log k)$ algorithm.