The Sorting/verifySortingOrder extension checks whether values are sorted in ascending or descending order and returns a result showing whether mismatches were found.
This is useful when a journey needs to verify that table, list, dropdown, or array values are displayed in the expected sorted order, either from a provided array or from values extracted from the page using XPath.
Parameters:
-
Arrayoptional, a JSON-formatted array string containing values to check for sorting. This is optional whenXpathis provided; -
Xpathoptional, a string containing an XPath query used to extract text values from the current document. This is optional whenArrayis provided; -
SortingOrderrequired, a string that must be eitherascendingordescending; -
StartRowrequired, a number indicating the 1-based row position from which sorting validation should begin;
Note: Provide either Array or Xpath. If Array is present and not blank, the script uses it first and parses it with JSON.parse(array).
NLP usage
Use the extension in a journey by calling Sorting/verifySortingOrder with the execute command. Pass each value to the matching extension input using as inputName.
Note: Sorting is checked using JavaScript localeCompare on the extracted text values. The returned object includes hasMismatches and either a success message or mismatch descriptions.
store value ${["Apple","Banana","Cherry","Date","Elderberry"]} in $asArray
execute "Sorting/verifySortingOrder" using $asArray as Array using "ascending" as SortingOrder using "1" as StartRow returning $statusexecute "Sorting/verifySortingOrder" using "//table[@id=\"example\"]//tbody//tr//td[1]" as Xpath using "ascending" as SortingOrder using "2" as StartRow returning $statusYou can also pass stored variables into the extension inputs.
execute "Sorting/verifySortingOrder" using "$asArray" as Array using "$order" as SortingOrder using "$startRow" as StartRow returning $statusstore value ${["Elderberry","Date","Cherry","Banana","Apple"]} in $asArray
store value "descending" in $order
store value "2" in $startRow
execute "Sorting/verifySortingOrder" using "$asArray" as Array using "$order" as SortingOrder using "$startRow" as StartRow returning $statusExample output when the values are correctly sorted:
{
"message": "The array is correctly sorted in descending order.",
"hasMismatches": false
}This extension does not require any external resource.
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: The script compares values as strings using localeCompare, so numeric-looking values are not treated as numbers. For example, string sorting may not match numeric sorting expectations. When using Xpath, the script depends on browser DOM XPath support and can only read nodes available in the current document. It returns mismatch descriptions for adjacent order breaks rather than reordering or correcting the source data.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 25/05/2026
Resources:
This extension does not require any external resource.
// Note this extension is not a product feature of Virtuoso and is not officially supported
// Extensions use javascript which may or may not be compatible with systems under test (SUTs)
// We welcome you to use this extension or adapt it for your needs
/*
The code enhances functionality by adding sorting checks for both ascending and descending order, identifying mismatches in sorting, and supporting data extraction from either an array or an XPath query, significantly improving versatility and robustness compared to the original code.
*/
function isSortedAscending(arr) {
for (let i = 1; i < arr.length; i++) {
if (arr[i][1].localeCompare(arr[i - 1][1]) < 0) {
return false;
}
}
return true;
}
function isSortedDescending(arr) {
for (let i = 1; i < arr.length; i++) {
if (arr[i][1].localeCompare(arr[i - 1][1]) > 0) {
return false;
}
}
return true;
}
function findMismatches(arr, sortOrder) {
let mismatches = [];
for (let i = 1; i < arr.length; i++) {
if ((sortOrder === 'ascending' && arr[i][1].localeCompare(arr[i - 1][1]) < 0) ||
(sortOrder === 'descending' && arr[i][1].localeCompare(arr[i - 1][1]) > 0)) {
mismatches.push({ index: i, value: arr[i][1], previousValue: arr[i - 1][1] });
}
}
return mismatches;
}
function extractDataUsingXPath(xpathQuery) {
let results = [];
let xpathResult = document.evaluate(xpathQuery, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < xpathResult.snapshotLength; i++) {
let node = xpathResult.snapshotItem(i);
results.push(node.textContent || node.innerText);
}
return results;
}
function checkSortingAndFindMismatches(array, xPath, sortingOrder, startRow) {
let optionValues = [];
try {
if (array && array.trim() !== '') {
optionValues = JSON.parse(array).map((item, index) => [index, item]);
} else if (xPath && typeof xPath === 'string') {
let extractedData = extractDataUsingXPath(xPath);
optionValues = extractedData.map((item, index) => [index, item]);
} else {
throw new Error("Input is required: either array or xPath must be provided.");
}
} catch (e) {
throw new Error("Error in processing input: " + e.message);
}
if (!sortingOrder || (sortingOrder !== 'ascending' && sortingOrder !== 'descending')) {
throw new Error("Invalid or missing sortingOrder. Please provide 'ascending' or 'descending'.");
}
// Adjust startRow to be zero-based
startRow = Math.max(startRow - 1, 0);
optionValues = optionValues.slice(startRow);
if (optionValues.length > 0) {
const isSorted = sortingOrder === 'ascending' ? isSortedAscending(optionValues) : isSortedDescending(optionValues);
if (isSorted) {
console.log("The array is correctly sorted in " + sortingOrder + " order.");
return { message: "The array is correctly sorted in " + sortingOrder + " order.", hasMismatches: false };
} else {
let mismatches = findMismatches(optionValues, sortingOrder);
let mismatchDescriptions = mismatches.map(m =>
`Mismatch at position ${m.index + startRow}: '${m.value}' should be ${sortingOrder === 'ascending' ? 'greater than' : 'less than'} '${m.previousValue}'.`
);
console.log("Found mismatches:", mismatchDescriptions);
return { mismatches: mismatchDescriptions, hasMismatches: true };
}
} else {
throw new Error("No valid data to sort. Please provide a non-empty array or a valid XPath query.");
}
}
// Example usage
const array = Array; // Sample array
const xPath = Xpath; // Sample XPath query
const sortingOrder = SortingOrder; // or 'descending'
const startRow = StartRow; // Starting from the second row
const result = checkSortingAndFindMismatches(array, xPath, sortingOrder, startRow);
console.log(result);
return result;
Comments
0 comments
Please sign in to leave a comment.