Skip to content

Commit 9c097b6

Browse files
Revert "Support sorting iterators (#46104)" (#52010)
Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com> (cherry picked from commit 1cb85ad)
1 parent 2638522 commit 9c097b6

File tree

2 files changed

+2
-77
lines changed

2 files changed

+2
-77
lines changed

base/sort.jl

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module Sort
55
using Base.Order
66

77
using Base: copymutable, midpoint, require_one_based_indexing, uinttype,
8-
sub_with_overflow, add_with_overflow, OneTo, BitSigned, BitIntegerType, top_set_bit,
9-
IteratorSize, HasShape, IsInfinite, tail
8+
sub_with_overflow, add_with_overflow, OneTo, BitSigned, BitIntegerType, top_set_bit
109

1110
import Base:
1211
sort,
@@ -1470,12 +1469,6 @@ end
14701469
14711470
Variant of [`sort!`](@ref) that returns a sorted copy of `v` leaving `v` itself unmodified.
14721471
1473-
Returns something [`similar`](@ref) to `v` when `v` is an `AbstractArray` and uses
1474-
[`collect`](@ref) to support arbitrary non-`AbstractArray` iterables.
1475-
1476-
!!! compat "Julia 1.10"
1477-
`sort` of arbitrary iterables requires at least Julia 1.10.
1478-
14791472
# Examples
14801473
```jldoctest
14811474
julia> v = [3, 1, 2];
@@ -1493,39 +1486,7 @@ julia> v
14931486
2
14941487
```
14951488
"""
1496-
function sort(v; kws...)
1497-
size = IteratorSize(v)
1498-
size == HasShape{0}() && throw(ArgumentError("$v cannot be sorted"))
1499-
size == IsInfinite() && throw(ArgumentError("infinite iterator $v cannot be sorted"))
1500-
sort!(collect(v); kws...)
1501-
end
1502-
sort(v::AbstractVector; kws...) = sort!(copymutable(v); kws...) # for method disambiguation
1503-
sort(::AbstractString; kws...) =
1504-
throw(ArgumentError("sort(::AbstractString) is not supported"))
1505-
sort(::Tuple; kws...) =
1506-
throw(ArgumentError("sort(::Tuple) is only supported for NTuples"))
1507-
1508-
function sort(x::NTuple{N}; lt::Function=isless, by::Function=identity,
1509-
rev::Union{Bool,Nothing}=nothing, order::Ordering=Forward) where N
1510-
o = ord(lt,by,rev,order)
1511-
if N > 9
1512-
v = sort!(collect(x), DEFAULT_STABLE, o)
1513-
tuple((v[i] for i in 1:N)...)
1514-
else
1515-
_sort(x, o)
1516-
end
1517-
end
1518-
_sort(x::Union{NTuple{0}, NTuple{1}}, o::Ordering) = x
1519-
function _sort(x::NTuple, o::Ordering)
1520-
a, b = Base.IteratorsMD.split(x, Val(length(x)>>1))
1521-
merge(_sort(a, o), _sort(b, o), o)
1522-
end
1523-
merge(x::NTuple, y::NTuple{0}, o::Ordering) = x
1524-
merge(x::NTuple{0}, y::NTuple, o::Ordering) = y
1525-
merge(x::NTuple{0}, y::NTuple{0}, o::Ordering) = x # Method ambiguity
1526-
merge(x::NTuple, y::NTuple, o::Ordering) =
1527-
(lt(o, y[1], x[1]) ? (y[1], merge(x, tail(y), o)...) : (x[1], merge(tail(x), y, o)...))
1528-
1489+
sort(v::AbstractVector; kws...) = sort!(copymutable(v); kws...)
15291490

15301491
## partialsortperm: the permutation to sort the first k elements of an array ##
15311492

test/sorting.jl

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,6 @@ end
8888
vcat(2000, (x:x+99 for x in 1900:-100:100)..., 1:99)
8989
end
9090

91-
function tuple_sort_test(x)
92-
@test issorted(sort(x))
93-
length(x) > 9 && return # length > 9 uses a vector fallback
94-
@test 0 == @allocated sort(x)
95-
end
96-
@testset "sort(::NTuple)" begin
97-
@test sort((9,8,3,3,6,2,0,8)) == (0,2,3,3,6,8,8,9)
98-
@test sort((9,8,3,3,6,2,0,8), by=x->x÷3) == (2,0,3,3,8,6,8,9)
99-
for i in 1:40
100-
tuple_sort_test(tuple(rand(i)...))
101-
end
102-
@test_throws ArgumentError sort((1,2,3.0))
103-
end
104-
10591
@testset "partialsort" begin
10692
@test partialsort([3,6,30,1,9],3) == 6
10793
@test partialsort([3,6,30,1,9],3:4) == [6,9]
@@ -544,28 +530,6 @@ end
544530
@test isequal(a, [8,6,7,NaN,5,3,0,9])
545531
end
546532

547-
@testset "sort!(iterable)" begin
548-
gen = (x % 7 + 0.1x for x in 1:50)
549-
@test sort(gen) == sort!(collect(gen))
550-
gen = (x % 7 + 0.1y for x in 1:10, y in 1:5)
551-
@test sort(gen; dims=1) == sort!(collect(gen); dims=1)
552-
@test sort(gen; dims=2) == sort!(collect(gen); dims=2)
553-
554-
@test_throws ArgumentError("dimension out of range") sort(gen; dims=3)
555-
556-
@test_throws UndefKeywordError(:dims) sort(gen)
557-
@test_throws UndefKeywordError(:dims) sort(collect(gen))
558-
@test_throws UndefKeywordError(:dims) sort!(collect(gen))
559-
560-
@test_throws ArgumentError sort("string")
561-
@test_throws ArgumentError("1 cannot be sorted") sort(1)
562-
563-
@test sort(Set((1, 3, 6))) == [1, 3, 6]
564-
@test sort(Dict((1=>9, 3=>2, 6=>5))) == [1=>9, 3=>2, 6=>5]
565-
@test sort(keys(Dict((1=>2, 3=>5, 6=>9)))) == [1, 3, 6]
566-
@test sort(values(Dict((1=>9, 3=>2, 6=>5)))) == [2, 5, 9]
567-
end
568-
569533
@testset "sort!(::AbstractVector{<:Integer}) with short int range" begin
570534
a = view([9:-1:0;], :)::SubArray
571535
sort!(a)

0 commit comments

Comments
 (0)