Sort a heap before outputting it