Check to see if an array is already sorted?

前端 未结 8 1201
渐次进展
渐次进展 2020-12-17 08:52

I know how to put an array in order, but in this case I just want to see if it is in order. An array of strings would be the easiest, I imagine, and answer

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 09:28

    You can compare them two by two:

    [["a", 3],["b",53],["c",2]].each_cons(2).all?{|p, n| (p <=> n) != 1} # => true
    

提交回复
热议问题