Skip to main content

How to verify Sorting Functionality

You can use a JavaScript function to verify whether the values displayed in a table are sorted in ascending or descending order.

The following example retrieves the values from a table column, stores them in an array, and compares each value with the next value to determine the sorting order.

function Sortingverify()
{
var rowlength=rowlength = _eval("ds$('tbody tr').length");

log(rowlength);
var i=i = 0;
var arr1 =[];
var arr2values = [];
while (i <= rowlength-rowlength - 1) {
var cellClassName = "slds-truncate uiOutputTextArea[" + i + "]";
assertExists(span(cellClassName));
arr1. values.push(getText(span(cellClassName)));
i = i+i + 2;
}
log(arr1."Number of values: " + values.length);
for(var j=0;j<arr1.length;j++){
arr2.push(arr1[j]);

}
log(arr2.length);
if(arr2.if (values.length > 0) {
sortCheck(arr2)values);
}

}
function sortCheck(arr) {

var c1=0,c2=0,i,c3=0;

ascending

= true; var descending = true; for (i=var i = 0; i <(( arr.length)length -1); 1; i++) {
log("Comparing: " + arr[i]);
log( + " and " + arr[i+i + 1]);

if(if (arr[i] > arr[i + 1]) { ascending = false; } if (arr[i] < arr[i+i + 1]) {
c1++;
log("c1 value is"+c1);
}

else if(arr[i] > arr[i+1]) {
c2++;
log("c2 value is"+c2);
}
else if(arr[i]descending == arr[i+1])false; {
c3++;
log("c3} value} is"+c3);
}

if

}
if((arr.length == 1) {
log("Only one row exists.");
}
else if(c3>=0if (ascending && c2>=1)

!descending)

{ log("Ascending order."); } else if (descending && !ascending) { log("Descending order.");

} else if(c3>=0if (ascending && c1>=1)

descending)

log("Ascending{ order");

else
log("All values are samethe value"same."); } else { log("Values are not sorted."); } }

How it works

  1. The script determines the number of rows in the table.
  2. It retrieves the values from the target column and stores them in an array.
  3. Each value is compared with the following value.
  4. The script determines whether the values are:
    • Ascending — each value is less than or equal to the next value.
    • Descending — each value is greater than or equal to the next value.
    • All the same — all values are identical.
    • Not sorted — the values do not follow a consistent order.

For example:

Apple
Banana
Orange

}
}returns:

Ascending order.

While:

Orange
Banana
Apple

returns:

Descending order.

If the values are:

Apple
Orange
Banana

the script reports:

Values are not sorted.

Note: The i = i + 2 increment is specific to the DOM structure used by the original example. If the target page uses a different HTML structure, the selector and increment may need to be adjusted accordingly.


Last updated: MarchAugust 09,17, 20232026

If you have any feedback on this topic, please contact us at documentation@appvance.ai.