Simplifying sorting logic