While the answers of AnorZaken and Greg Beech are very nice, since they do not require the use of the extension method, it is sometimes useful to avoid Zip (), as some enumerations can be expensive to enumerate in this way.
The solution can be found in Aggregate ()
double[] score1 = new double[] { 12.2, 13.3, 5, 17.2, 2.2, 4.5 }; double[] score2 = new double[] { 2.2, 4.5, 5, 12.2, 13.3, 17.2 }; bool isordered1 = score1.Aggregate(double.MinValue,(accum,elem)=>elem>=accum?elem:double.MaxValue) < double.MaxValue; bool isordered2 = score2.Aggregate(double.MinValue,(accum,elem)=>elem>=accum?elem:double.MaxValue) < double.MaxValue; Console.WriteLine ("isordered1 {0}",isordered1); Console.WriteLine ("isordered2 {0}",isordered2);
One thing a little ugly regarding the above solution is the double comparison less. Floating comparisons like this make me nauseous as it is almost like comparing floating point equality. But it seems he works here twice. Integer values โโwill also be exact. Floating point comparisons can be avoided by using types with a null value, but then the code becomes a little harder to read.
double[] score3 = new double[] { 12.2, 13.3, 5, 17.2, 2.2, 4.5 }; double[] score4 = new double[] { 2.2, 4.5, 5, 12.2, 13.3, 17.2 }; bool isordered3 = score3.Aggregate((double?)double.MinValue,(accum,elem)=>(elem>(accum??(double?)double.MaxValue).Value)?(double?)elem:(double?)null) !=null; bool isordered4 = score4.Aggregate((double?)double.MinValue,(accum,elem)=>(elem>(accum??(double?)double.MaxValue).Value)?(double?)elem:(double?)null) !=null; Console.WriteLine ("isordered3 {0}",isordered3); Console.WriteLine ("isordered4 {0}",isordered4);