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.