I\'m interested if there is a way, in LINQ, to check if all numbers in a list are increasing monotonically?
Example
List l
Use a loop! It's short, fast and readable. With the exception of Servy's answer, most the solutions in this thread are unnecessarily slow (sorting takes 'n log n' time) .
// Test whether a sequence is strictly increasing.
public bool IsIncreasing(IEnumerable list)
{
bool initial = true;
double last = Double.MinValue;
foreach(var x in list)
{
if (!initial && x <= last)
return false;
initial = false;
last = x;
}
return true;
}
IsIncreasing(new List{1,2,3})
returns TrueIsIncreasing(new List{1,3,2})
returns False