Error proof sort by distance