@@ -4,15 +4,19 @@ using StatsBase
44using Random
55
66a = rand (1 : 10000 , 1000 )
7+ am = [rand () < .9 ? i : missing for i in a]
78
8- for alg in [TimSort, HeapSort, RadixSort, CombSort]
9+ for alg in [TimSort, HeapSort, RadixSort, CombSort, SortingAlgorithms . TimSortAlg () ]
910 b = sort (a, alg= alg)
1011 @test issorted (b)
1112 ix = sortperm (a, alg= alg)
1213 b = a[ix]
1314 @test issorted (b)
1415 @test a[ix] == b
1516
17+ # legacy 3-argument calling convention
18+ @test b == sort! (copy (a), alg, Base. Order. Forward)
19+
1620 b = sort (a, alg= alg, rev= true )
1721 @test issorted (b, rev= true )
1822 ix = sortperm (a, alg= alg, rev= true )
@@ -34,9 +38,26 @@ for alg in [TimSort, HeapSort, RadixSort, CombSort]
3438 invpermute! (c, ix)
3539 @test c == a
3640
37- if alg != RadixSort # RadixSort does not work with Lt orderings
41+ if alg != RadixSort # RadixSort does not work with Lt orderings or missing
3842 c = sort (a, alg= alg, lt= (> ))
3943 @test b == c
44+
45+ # Issue https://github.com/JuliaData/DataFrames.jl/issues/3340
46+ bm1 = sort (am, alg= alg)
47+ @test issorted (bm1)
48+ @test count (ismissing, bm1) == count (ismissing, am)
49+
50+ bm2 = am[sortperm (am, alg= alg)]
51+ @test issorted (bm2)
52+ @test count (ismissing, bm2) == count (ismissing, am)
53+
54+ bm3 = am[sortperm! (collect (eachindex (am)), am, alg= alg)]
55+ @test issorted (bm3)
56+ @test count (ismissing, bm3) == count (ismissing, am)
57+
58+ if alg == TimSort # Stable
59+ @test all (bm1 .=== bm2 .=== bm3)
60+ end
4061 end
4162
4263 c = sort (a, alg= alg, by= x-> 1 / x)
@@ -103,8 +124,8 @@ for n in [0:10..., 100, 101, 1000, 1001]
103124 # test float sorting with NaNs
104125 s = sort (v, alg= alg, order= ord)
105126 @test issorted (s, order= ord)
106-
107- # This tests that NaNs (which compare equivalent) are treated stably
127+
128+ # This tests that NaNs (which compare equivalent) are treated stably
108129 # even when the underlying algorithm is unstable. That it happens to
109130 # pass is not a part of the public API:
110131 @test reinterpret (UInt64, v[map (isnan, v)]) == reinterpret (UInt64, s[map (isnan, s)])
0 commit comments