@@ -573,9 +573,11 @@ function _sort!(v::AbstractVector, a::MissingOptimization, o::Ordering, kw)
573
573
if nonmissingtype (eltype (v)) != eltype (v) && o isa DirectOrdering
574
574
lo, hi = send_to_end! (ismissing, v, o; lo, hi)
575
575
_sort! (WithoutMissingVector (v, unsafe= true ), a. next, o, (;kw... , lo, hi))
576
- elseif eltype (v) <: Integer && o isa Perm{DirectOrdering} && nonmissingtype (eltype (o. data)) != eltype (o. data)
576
+ elseif eltype (v) <: Integer && nonmissingtype (eltype (o. data)) != eltype (o. data) &&
577
+ (o isa Perm{DirectOrdering} || o isa PermUnstable{DirectOrdering})
578
+ PermT = o isa Perm{DirectOrdering} ? Perm : PermUnstable
577
579
lo, hi = send_to_end! (i -> ismissing (@inbounds o. data[i]), v, o)
578
- _sort! (v, a. next, Perm (o. order, WithoutMissingVector (o. data, unsafe= true )), (;kw... , lo, hi))
580
+ _sort! (v, a. next, PermT (o. order, WithoutMissingVector (o. data, unsafe= true )), (;kw... , lo, hi))
579
581
else
580
582
_sort! (v, a. next, o, kw)
581
583
end
@@ -614,15 +616,17 @@ function _sort!(v::AbstractVector, a::IEEEFloatOptimization, o::Ordering, kw)
614
616
else
615
617
_sort! (iv, a. next, Forward, (;kw... , lo= j+ 1 , hi, scratch))
616
618
end
617
- elseif eltype (v) <: Integer && o isa Perm && o. order isa DirectOrdering && is_concrete_IEEEFloat (eltype (o. data))
619
+ elseif eltype (v) <: Integer && (o isa Perm || o isa PermUnstable) &&
620
+ o. order isa DirectOrdering && is_concrete_IEEEFloat (eltype (o. data))
618
621
lo, hi = send_to_end! (i -> isnan (@inbounds o. data[i]), v, o. order, true ; lo, hi)
619
622
ip = reinterpret (UIntType (eltype (o. data)), o. data)
620
623
j = send_to_end! (i -> after_zero (o. order, @inbounds o. data[i]), v; lo, hi)
621
- scratch = _sort! (v, a. next, Perm (Reverse, ip), (;kw... , lo, hi= j))
624
+ PermT = o isa Perm ? Perm : PermUnstable
625
+ scratch = _sort! (v, a. next, PermT (Reverse, ip), (;kw... , lo, hi= j))
622
626
if scratch === nothing # Union split
623
- _sort! (v, a. next, Perm (Forward, ip), (;kw... , lo= j+ 1 , hi, scratch))
627
+ _sort! (v, a. next, PermT (Forward, ip), (;kw... , lo= j+ 1 , hi, scratch))
624
628
else
625
- _sort! (v, a. next, Perm (Forward, ip), (;kw... , lo= j+ 1 , hi, scratch))
629
+ _sort! (v, a. next, PermT (Forward, ip), (;kw... , lo= j+ 1 , hi, scratch))
626
630
end
627
631
else
628
632
_sort! (v, a. next, o, kw)
@@ -1482,7 +1486,7 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector,
1482
1486
end
1483
1487
1484
1488
# do partial quicksort
1485
- _sort! (ix, _PartialQuickSort (k), Perm (ord (lt, by, rev, order), v), (;))
1489
+ _sort! (ix, _PartialQuickSort (k), PermUnstable (ord (lt, by, rev, order), v), (;))
1486
1490
1487
1491
maybeview (ix, k)
1488
1492
end
@@ -1554,7 +1558,8 @@ function sortperm(A::AbstractArray;
1554
1558
end
1555
1559
end
1556
1560
ix = copymutable (LinearIndices (A))
1557
- sort! (ix; alg, order = Perm (ordr, vec (A)), scratch, dims... )
1561
+ PermT = alg == DEFAULT_STABLE ? PermUnstable : Perm
1562
+ sort! (ix; alg, order = PermT (ordr, vec (A)), scratch, dims... )
1558
1563
end
1559
1564
1560
1565
@@ -1608,7 +1613,8 @@ function sortperm!(ix::AbstractArray{T}, A::AbstractArray;
1608
1613
if ! initialized
1609
1614
ix .= LinearIndices (A)
1610
1615
end
1611
- sort! (ix; alg, order = Perm (ord (lt, by, rev, order), vec (A)), scratch, dims... )
1616
+ PermT = alg == DEFAULT_STABLE ? PermUnstable : Perm
1617
+ sort! (ix; alg, order = PermT (ord (lt, by, rev, order), vec (A)), scratch, dims... )
1612
1618
end
1613
1619
1614
1620
# sortperm for vectors of few unique integers
0 commit comments