fixed sorting bug

Hey Captain, if you're interested, this fix for the bug seems simple and unintrusive enough to me. Let me know what you think.