## Checking the content of an array with comparison operators, concerning a specified order

Related searches

I need a function which checks an array with integers. The integers have to be either in ascending order or in descending order. That depends from case to case.

I have figured out the following solution:

```const areVersionNumbersInExpectedOrder = (aNums, sExpectedOrder = "ascending") => {
let passed = false;

if (sExpectedOrder === "ascending") {
if (aNums[0] < aNums[1] && aNums[1] < aNums[2]) {
passed = true;
}
} else if (sExpectedOrder === "descending") {
if (aNums[0] > aNums[1] && aNums[1] > aNums[2]) {
passed = true;
}
}

return passed
};

const versionNumbersAscending = [1, 2, 3];
console.log(
`Result ascending order: \${areVersionNumbersInExpectedOrder(versionNumbersAscending, "ascending")}`);

const versionNumbersDescending = [3, 2, 1];
console.log(
`Result descending order: \${areVersionNumbersInExpectedOrder(versionNumbersDescending, "descending")}`);```

You could take a shorter approach with `every` and a callback depending on the expected order.

```const areVersionNumbersInExpectedOrder = (aNums, sExpectedOrder = "ascending") => {
const
order = {
ascending: (b, i, { [i - 1]: a }) => !i || a < b,
descending: (b, i, { [i - 1]: a }) => !i || a > b
};

return aNums.every(order[sExpectedOrder]);
};

console.log(areVersionNumbersInExpectedOrder([1, 2, 3], "ascending"));
console.log(areVersionNumbersInExpectedOrder([3, 2, 1], "descending"));```

about_Comparison_Operators, To use a comparison operator, specify the values that you want to -in, Returns true when test value contained in a If there are no matches in a collection, comparison operators return an empty array. If you need to compare a value to \$null you should put \$null on the left-hand side of the comparison. Comparison operators compare two values, returning true if the comparison holds or false otherwise. The operands must be of the same type (except for integers and floats which are interchangeable), otherwise the comparison returns null as usual for type conflicts. If any operand is null, the comparison returns null.

how about something like this:

```const isSortedAscending = (v, i, a) => i === 0 || v >= a[i - 1];
const isSortedDescending = (v, i, a) => i === 0 || v <= a[i - 1];

const versionNumbersAscending = [1, 2, 3];
console.log(`Result ascending order: %o`, versionNumbersAscending.every(isSortedAscending));

const versionNumbersDescending = [3, 2, 1];
console.log(`Result descending order: %o`, versionNumbersDescending.every(isSortedDescending));

const versionNumbersAscending2 = [1, 2, 3, 3, 4, 5, 6, 6, 6, 7, 8, 9];
console.log(`arbitrary length arrays: %o`, versionNumbersAscending2.every(isSortedAscending));

console.log(`should fail: %o`, [1, 2, 3, 2, 4, 5].every(isSortedAscending));```

JavaScript Array every(): Determining If All Array Elements Pass a Test, You will learn how to use the JavaScript Array every() method to test whether all you need to test whether every element of an array satisfies a specified condition. every() method executes the callback() function on every element in the array Syntax � Variables � Data Types � Number � Boolean. JavaScript Operators. OJAI supports comparison, existence, between, match, like, type of, size of, in, and logical operators. OJAI Query Condition Examples This section contains examples that show you how to use different OJAI query condition operators in combination with different field references and data types.

I think this is better:

```const isInOrder = (arr, isAscending = true) => {
let result = false;
for (let i = 0; i < arr.length - 1; i++) {
result = isAscending ? arr[i] <= arr[i + 1] : arr[i] >= arr[i + 1];
if (!result) {
return result;
}
}
return result;
};
```

JavaScript Array sort: Sorting an Array More Effectively, If you omit the compare function, the sort() method sorts the elements with the sort order based on the Unicode code point values of elements as mentioned� Comparison/Sort Order; MongoDB Extended JSON (v2) Array Query Operators Selects documents if the array field is a specified size.

Equality comparisons and sameness, indexOf , Array.prototype. Which operation you choose depends on what sort of comparison of a not-a-number value, NaN , to represent the solution to certain any values of A and B (except for the order of applied conversions). with the exception of doing a type check upfront in 11.9.6.1, the Strict� arr1.length == arr2.length && arr1.reduce( (a, b) => a && arr2.includes(b), true) If you want to compare also the equality of order: arr1.length == arr2.length && arr1.reduce( (a, b, i) => a && arr2[i], true) The length check ensures that the set of elements in one array isn't just a subset of the other one.

Determine equality - MATLAB eq ==, This MATLAB function returns a logical array with elements set to logical 1 The test compares both real and imaginary parts of numeric arrays. eq returns This leads to small differences in results that the == operator reflects. Perform a few subtraction operations on numbers expressed in decimal and store the result in C . Filter. Eq (< field >, < value >) <value> is the exact array to match, including the order of the elements. To specify equality condition on an array, use the query document { <field> => <value> } where <value> is the exact array to match, including the order of the elements.

##### Comments
• You're current solution depends on you knowing the array `length` before it is passed to your function. What happens when the array is longer / shorter than 3 elements? Your code would break or not return a true comparison. You should look at using a `for loop` to iterate each element in the array, comparing to the previous or next based on whether it is `descending` or `ascending` order. Also, if two elements are equal, they could still pass the test for ordering, so 1, 2, 2, 3, etc. having 2 < 2 would fail, even though the ordering is correct.
• @RyanWilson Thanks for your hint. But in the test-scenario, for which I need the function, it will be always 3 elements. Because it will be based on static test-data.
• While this may currently be true, you may want to try and learn to write your code as dynamically as possible to account for things which could become problematic in the future. If you go with the `for loop` solution, you won't have to worry about changing it later if your array ever grows or shrinks.
• @RyanWilson Yes, you are right with what you say there, about more dynamic. I really should iterate over my current implemention ...
• Thanks a lot. I really like your solution. The idea is great. But sadly I have to hint: It would fail, if the order is incorrect somewhere in the middle of the array. And the consecutive elements are in correct order again. Please try it with this array : const nums = [1, 9, 3, 4, 5, 6];
• You are right thanks, we need to return in the bottom of for loop if result === false.