the issue is with signed vs unsigned. as the other comment said, using - instead of <=> is the main bug. this is not a perl bug. but if you do need speed, check out Sort::Maker as it will do that sort faster than your code as it will factor out all those hash lookups before the sort is done.