Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #353 Test if list is sorted

Set the boolean b to true if the list items has all its elements in increasing order, false otherwise.

The order is not strict: the ordered list may contain duplicate elements.

b = items.each_cons(2).all?{|a,b| a <= b }
b = items.each_cons(2).all?{ _1 <= _2 } 
import "slices"
b := slices.IsSorted(items)
b = sorted(items) == items

New implementation...
< >
programming-idioms.org