# easy way to check ordering of numbers?

• 02-27-2008
Marksman
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?
• 02-27-2008
dudeomanodude
Yes, a sort() will accomplish a sort. But what do you mean by "order"? Like as in a polynomial?
• 02-27-2008
Marksman
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
• 02-27-2008
dudeomanodude
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.
• 02-27-2008
Marksman
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
• 02-27-2008
dudeomanodude
Quote:

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?
• 02-27-2008
Marksman
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.
• 02-28-2008
hk_mp5kpdw
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).