tricksadvanced
Check Sorting
Check if the input array is sorted in ascending or descending order, or is unsorted. The logic is implemented within a custom Scripting Rule.
David Škarka
Template author
This Complex Rule verifies the organization of data within a list. It analyzes an input array to determine if the elements are arranged in a specific sequence (ascending or descending), if they are equal, or if no sorting pattern exists.
Solution components
- A Scripting Rule named Check Sorting executes custom logic to assess the order of elements provided in the input.
- Input validation logic first checks the volume of data. If the input array contains fewer than two items, the rule returns a specific status indicating insufficient data to determine an order.
- Iterative comparison logic cycles through the array comparing adjacent elements:
- It ignores adjacent identical elements.
- It establishes an initial direction (ascending or descending) based on the first unequal pair found.
- If subsequent pairs contradict the established direction, the rule immediately identifies the list as "unsorted".
- Final status determination assigns a final output string based on the analysis:
- "ascending": If all comparable elements increase in value.
- "descending": If all comparable elements decrease in value.
- "all items are equal": If no difference in value was found across the entire array.
- "unsorted": If the elements follow no consistent order.
- "not enough items": If the array length is less than 2.
- Output formatting ensures the result is returned in the output field, while preserving the original data in the inputValues field for reference.
More Templates
See Other Templates
financial servicessuggested
A/B Testing
Discover a simple way to assign a test group for performing A/B Testing on your decisions.
financial servicessuggested
Risk Based Pricing
Discover a simple way to apply a suitable Risk Based Pricing matrix based on a type of client.
financial servicessuggested
Eligibility and Policy Rules
Discover a simple way to define and execute various sets of rules and get the final decision outcome.