rewrite function for sorting manpath
This makes the sorting more robust in some edge cases. Also, the code is more readable.