in reply to sort while push in hash

Looks like you need a heap instead. Look for the Heap:: modules at CPAN (or, my preferred choice, Array::Heap2).