View source code
Display the source code in std/algorithm/sorting.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.algorithm.sorting.topNCopy

Copies the top n elements of the input range source into the random-access range target, where n = target.length.

TRange topNCopy(alias less, SRange, TRange) (
  SRange source,
  TRange target,
  SortOutput sorted = No.sortOutput
if (isInputRange!SRange && isRandomAccessRange!TRange && hasLength!TRange && hasSlicing!TRange);

Elements of source are not touched. If sorted is true, the target is sorted. Otherwise, the target respects the heap property.


less The predicate to sort by.
source The source range.
target The target range.
sorted Whether to sort the elements copied into target.


The slice of target containing the copied elements.


import std.typecons : Yes;

int[] a = [ 10, 16, 2, 3, 1, 5, 0 ];
int[] b = new int[3];
topNCopy(a, b, Yes.sortOutput);
writeln(b); // [0, 1, 2]


Andrei Alexandrescu


Boost License 1.0.