edoc,
I don't think (sort keys %hash) is what is being asked for. It appears the original order is to be preserved, but the elements on the line sorted. I missed this my first, second, and third time reading it. I would argue that yours is easier to read though :-)