Optimize the sorting of records

This optimizes the code a bit because it doesn't sort as many records as
previously. Only the records that will be in the final output are
actually sorted.