Thread: easy way to check ordering of numbers?

1. easy way to check ordering of numbers?

I want to check the ordering of numbers in an if statement, for example, in pseudo-code:
Code:
```if a < b < c < ... < z
then do something magical
else quit```
Is there an easy way to do this? Obviously doing all the boolean comparisons is not possible. Maybe sorting the numbers and then doing the respective comparisons is the best way?

2. Yes, a sort() will accomplish a sort. But what do you mean by "order"? Like as in a polynomial?

3. Oh sorry, by 'order' I meant just numerical order. For example
Code:
`3 < 4 < 5 < 6`
should be true and
Code:
` 3 < 5 < 4 < 6`
should be false

4. I would imagine the easiest way is to place these numbers in a list or vector, and traverse it checking if prev < curr.

If you make it to the end of the list, the order is correct.

5. sorry I must have had a brain fart ...
Code:
`a < b && b < c && c < d`
etc...

my post probably wasn't clear, I was just looking for that

.... I should probably take a break

6. Originally Posted by Marksman
sorry I must have had a brain fart ...
Code:
`a < b && b < c && c < d`
etc...
Well sure that will work for just four numbers, but what if you have 50 numbers? Will you write out all those comparison tests?

7. Yeah that's true. In my actual program I only had 4 variables, so it was manageable.
Otherwise the vector/list approach would definitely be better.

8. How are the numbers stored? In a list/array/vector of some kind? You can potentially keep track if the values are ordered as they are entered and simply have a flag (initialized to true for the empty container). Whenever you add a new number, simply check if it is more than the previous value - if not then set the flag to false meaning the list is not ordered. When you do your if test, then just check this flag (an O(1) operation).