max date record in LINQ

Starting from .NET 6 MaxBy LINQ method is available. var result = items.MaxBy(i => i.Date); Prior to .NET 6: O(n): var result = items.Aggregate((x, y) => x.Date > y.Date ? x : y); O(n log n): var result = items.OrderByDescending(i => i.Date).First(); O(n) – but iterates over the sequence twice: var max = items.Max(i => … Read more

Why does Math.min() return positive infinity, while Math.max() returns negative infinity?

Of course it would, because the start number should be Infinity for Math.min. All number that are lower than positive infinity should be the smallest from a list, if there are no smaller. And for Math.max it’s the same; all numbers that are larger than negative infinity should be the biggest if there are no … Read more

why is c++ std::max_element so slow?

Before voting on this answer, please test (and verify) this on your machine and comment/add the results. Note that I used a vector size of 1000*1000*1000 for my tests. Currently, this answer has 19 upvotes but only one posted results, and these results did not show the effect described below (though obtained with a different … Read more