Fixing the implementation of sort and sort_by