The comparison function used with `Array.sort` is supposed to return (negative/zero/positive) for `a` (less/equal/greater) `b`, so I think you want to subtract the distances here.