How to get the latest date from a DateTime collection?


I have got array of with type DateTime DateTime [] dts; How I can get the latest date from it?

There is no need for sorting. And using minPos might not be the most convenient way to get the largest value, as it returns a range

You could just use reduce with max as a predicate. This approach will work with any range or comparable elements, not just DateTime

import std.datetime;
import std.algorithm;
import std.stdio;

void main(){
    DateTime[] times;   // array of DateTimes filled with dummy data
    foreach (i; 0 .. 5) {
        times ~= DateTime() + dur!"seconds"(2 * i);

    auto t = times.reduce!max;

    writefln("All Times: %s", times);
    writefln("Most Recent Time: %s", t);