# 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.

```javascript
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:

```text
Apple
Banana
Orange
```

returns:

```text
Ascending order.
```

While:

```text
Orange
Banana
Apple
```

returns:

```text
Descending order.
```

If the values are:

```text
Apple
Orange
Banana
```

the script reports:

```text
Values are not sorted.
```

> **Note:** The `<span class="editor-theme-code">i = i + 2</span>` 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>.