From d70356e0190d92b5e0f7e2389006911901ccd0f8 Mon Sep 17 00:00:00 2001 From: Infiltrator Date: Tue, 8 Dec 2015 17:57:01 +1100 Subject: [PATCH] Fix topN(Range, Range) disrespect of its predicate --- std/algorithm/sorting.d | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/std/algorithm/sorting.d b/std/algorithm/sorting.d index a43a15d05..8c042d3eb 100644 --- a/std/algorithm/sorting.d +++ b/std/algorithm/sorting.d @@ -2301,7 +2301,7 @@ auto topN(alias less = "a < b", static assert(ss == SwapStrategy.unstable, "Stable topN not yet implemented"); - auto heap = BinaryHeap!Range1(r1); + auto heap = BinaryHeap!(Range1, less)(r1); for (; !r2.empty; r2.popFront()) { heap.conditionalInsert(r2.front); @@ -2326,6 +2326,16 @@ unittest assert(t == [ 0, 1, 2, 2, 3 ]); } +// bug 15420 +unittest +{ + int[] a = [ 5, 7, 2, 6, 7 ]; + int[] b = [ 2, 1, 5, 6, 7, 3, 0 ]; + topN!"a > b"(a, b); + sort!"a > b"(a); + assert(a == [ 7, 7, 7, 6, 6 ]); +} + /** Copies the top $(D n) elements of the input range $(D source) into the random-access range $(D target), where $(D n =