Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting Numerics with a non-total order is unstable by default. #42713

Closed
LilithHafner opened this issue Oct 20, 2021 · 1 comment · Fixed by #45222
Closed

Sorting Numerics with a non-total order is unstable by default. #42713

LilithHafner opened this issue Oct 20, 2021 · 1 comment · Fixed by #45222
Labels
docs This change adds or pertains to documentation sorting Put things in order

Comments

@LilithHafner
Copy link
Member

LilithHafner commented Oct 20, 2021

This is unfortunate:

julia> sort(collect(1:22), by=x->0)
22-element Vector{Int64}:
 12
 21
 20
 19
 18
 17
 16
 15
 14
 13
  1
 11
 10
  9
  8
  7
  6
  5
  4
  3
  2
 22

We have

defalg(v::AbstractArray{<:Union{Number, Missing}}) = DEFAULT_UNSTABLE

because we assume that stable and unstable are indistinguishable when sorting numeric data. Unfortunately, this is only true for total orderings.

@LilithHafner LilithHafner changed the title Sorting Numerics with a non-antisymmetric order is unstable by default. Sorting Numerics with a non-total order is unstable by default. Oct 22, 2021
@simeonschaub
Copy link
Member

This could probably be better documented, but it is the intended behavior. If you want a stable algorithm, you need to explicitly request it:

julia> sort([1:22;]; by=x->0, alg=Base.DEFAULT_STABLE)
22-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation sorting Put things in order
Projects
None yet
2 participants