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 = _eval("ds$('tbody tr').length");

    log(rowlength);

    var i = 0;
    var values = [];

    while (i <= rowlength - 1)
    {
        var cellClassName = "slds-truncate uiOutputTextArea[" + i + "]";

        assertExists(span(cellClassName));

        values.push(getText(span(cellClassName)));

        i = i + 2;
    }

    log("Number of values: " + values.length);

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

function sortCheck(arr)
{
    var ascending = true;
    var descending = true;

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

        if (arr[i] > arr[i + 1])
        {
            ascending = false;
        }

        if (arr[i] < arr[i + 1])
        {
            descending = false;
        }
    }

    if (arr.length == 1)
    {
        log("Only one row exists.");
    }
    else if (ascending && !descending)
    {
        log("Ascending order.");
    }
    else if (descending && !ascending)
    {
        log("Descending order.");
    }
    else if (ascending && descending)
    {
        log("All values are the 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: August 17, 2026

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