From ?is.unsorted :
Check if the object is sorted (in ascending order) ...
So in this case, you could:
is.sorted = Negate(is.unsorted) is.sorted(x) #[1] TRUE #> is.sorted(1:5) #[1] TRUE #> is.sorted(5:1) #[1] FALSE #> is.sorted(sample(5)) #[1] FALSE #> is.sorted(sort(runif(5))) #[1] TRUE #> is.sorted(c(1,2,2,3)) #[1] TRUE #> is.sorted(c(1,2,2,3), strictly = T) #[1] FALSE
This function is fast because it crosses the vector and breaks the loop as soon as the element is not "> =" (or ">" if "strictly = T") from the previous one.
source share