Note that while second answer produces your test output, it does NOT sort the keys numerically. Show this by strengthening your test with another key=>val pair 100=>100 and see that 100 sorts before 40.
Originally posted as a Categorized Answer.
Comment on Re: how do i sort hash by key numerically