Distance sorting