Distance should always be the secondary sort criterion (#5245)