Fix sort problems in the backend