# How to sort 6 or more columns in a grid

**URL:** <https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761>\
**Category:** Browser\
**Created:** [2 September 2024 09:26 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761 "2024-09-02T09:26:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [2 September 2024 09:26 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/1 "2024-09-02T09:26:18Z")

</div>

I have a table that has 6 columns to sort, sort works when we click on the sort button present at the header of every column

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/f/f9510bf8e058cda7a93475eb612413566ea2b113.png)

Once we click on sort button, columns will sort the items in such a way that blank item will be at top and then the alphabetical order will start from A-z, for numbers it will be numeric sorting.

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [2 September 2024 09:27 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/2 "2024-09-02T09:27:40Z")

</div>

in the above screen shot there are only 4 columns, but in few other tables there could be 6 columns as well

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [2 September 2024 10:33 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/3 "2024-09-02T10:33:12Z")

</div>

@dominique Can you share the HTML which contains the columns?

---

<div class="post-metadata">

**Author:** ![CharlieScene](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/charliescene/32/3484_2.png) [@CharlieScene](https://forum.robotframework.org/u/CharlieScene)\
**Post date:** [2 September 2024 16:32 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/4 "2024-09-02T16:32:05Z")

</div>

Hi,

According to your screen, you have a change in the symbol of the sorted columns, so you should verify if it has an attribute in this case (for example **sort** with a ‘up’ or ‘down’, ‘asc’ or ‘desc’ value).

Then you could get info depending of the columns names and click until the attribute is as expected.  
Something like this:

```
Table Sort
     [Arguments] ${col_name} ${sort_type}
     VAR ${isSorted} ${EMPTY}
     WHILE '${isSorted}' != '${sort_type}'
         Click Element //div[@sort and text()='${col_name}']
         ${isSorted} Get Element Attribute //div[@sort and text()='${col_name}'] sort
       END

```

Of course check the structure of the table and exact xpath you can use.

Regards  
Charlie

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [3 September 2024 03:34 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/9 "2024-09-03T03:34:31Z")

</div>

Hi @falcon030 not sure when I’m sending the HTML, it it coming out blank, please find the screen shot in the reply section

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [3 September 2024 03:36 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/10 "2024-09-03T03:36:27Z")

</div>

Hi Charlie, I also want to check in the first 5 and last 5 rows of the column for the elements to be sorted alphabetically or numerically:

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [3 September 2024 03:52 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/11 "2024-09-03T03:52:42Z")

</div>

For some reason, I’m not able to post the HTML structure here, so sending a photo of it:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/1/17e65673fdeec11138a4d8bdf5d1fceb002f3c37.png)

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [3 September 2024 08:18 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/12 "2024-09-03T08:18:08Z")

</div>

Hi Dominique,

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/4/4eaa8a7256d45eed2419b7579ec556986824da06.png)

Do these `sortamountupalticon` change to `sortamountdownalticon` when you change the sort direction? That would be my guess, if so apply that to @CharlieScene’s example.

```python
sortamountupalticon
sortamountdownalticon

sort amount up alt icon
sort amount down alt icon

```

Dave.

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [3 September 2024 09:44 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/13 "2024-09-03T09:44:27Z")

</div>

@dominique Can you also share the HTML code where the names of the columns are visible?

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [3 September 2024 14:01 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/15 "2024-09-03T14:01:20Z")

</div>

Hi @falcon030 here is the HTML code with the names of columns that visible, I have removes some lines from the middle to capture in screenshot:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/c/c992b83e7837cf151c4431b1c156ca2b2b67254b.png)

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [3 September 2024 14:29 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/16 "2024-09-03T14:29:29Z")

</div>

Hi @damies13,

I also need to check if elements present in first page for each columns are sorted or not, I need to do sorting with actual elements present in the cells for each element in the first page, when we click on sort the first few sorted elements comes as blank and the rest will start alphabetically from A to Z, can you also help in validating this.

Dominique

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [3 September 2024 14:41 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/17 "2024-09-03T14:41:19Z")

</div>

@dominique I don’t see the names of the columns in the HTML code.  
Is it very different than this?

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/4/49c7f4c397bdd2dd2169f5804b164f47b6157057.png)

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [3 September 2024 15:27 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/18 "2024-09-03T15:27:45Z")

</div>

Hi @falcon030,

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/4/404c29fac2d358da94ad9a2a0af43f027b0aaa16.png)

In the above snap if you look at attribute “field” which is highlighted that would be the column name for all the columns.

Dominique

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [3 September 2024 16:01 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/19 "2024-09-03T16:01:45Z")

</div>

@dominique That is not the same as text that is visible before the sort and filter icons. The value of the field attribute isn’t been displayed.

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [3 September 2024 19:38 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/22 "2024-09-03T19:38:36Z")

</div>

@dominique Yes that’s exactly what I needed 😃. Now what do you exactly want to do? Sort a column based on there name ascending or descending?

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [4 September 2024 00:35 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/23 "2024-09-04T00:35:13Z")

</div>

@dominique I think that you can use the following code. The keyword takes 2 embedded keywords: the visible name of the column and the sort order (Ascending / Descending).

```
sorttest
    New Browser headless=False
    New Context
    New Page url=Haven't the faintest idea
    Sort Column "FI Name" By "Descending" Order
    Sleep 5s

***Keywords***
Sort Column "${column_name}" By "${sort_order}" Order
    [Arguments] ${occurrence}=1
    ${sort_order} Convert To Lowercase ${sort_order}
    IF "${sort_order}" == "ascending"
            VAR ${sort_icon} sortamountupalticon
    END
    IF "${sort_order}" == "descending"
            VAR ${sort_icon} sortamountdownicon
    END
    ${sorted} Get Element States
    ... :nth-match(th[role="columnheader"]:has-text("${column_name}"):visible,${occurrence}):has(${sort_icon})
    ... then
    ... bool(value & visible)
    WHILE ${sorted} == ${False}
        Click :nth-match(th[role="columnheader"]:has-text("${column_name}"):visible,${occurrence})
        Log To Console Matching= :nth-match(th[role="columnheader"]:has-text("${column_name}"):visible,${occurrence}):has(${sort_icon})
        ${sorted} Get Element States
        ... :nth-match(th[role="columnheader"]:has-text("${column_name}"):visible,${occurrence}):has(${sort_icon})
        ... then
        ... bool(value & visible)
    END

```

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [4 September 2024 03:34 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/24 "2024-09-04T03:34:49Z")

</div>

> [@falcon030](#):
>
> ascending or descending

Yes @falcon030, exactly once user clicks on sort button then I want to sort based on ascending or descending

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [4 September 2024 06:20 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/25 "2024-09-04T06:20:37Z")

</div>

Hi @falcon030,

When I use the keyword mentioned above, I get “Undefined Variable” for sort\_icon, you can see it in the screenshot below,

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/d/d2028ade7f03a2142d234566d1b31e267022b188.png)

can I declare the variable in some different way.

---

<div class="post-metadata">

**Author:** ![dominique](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@dominique](https://forum.robotframework.org/u/dominique)\
**Post date:** [4 September 2024 07:18 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/28 "2024-09-04T07:18:23Z")

</div>

![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/7/70e6e2bae6aceb81e97baf45c8694cf82b0ece70.png)

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [4 September 2024 12:26 UTC](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761/29 "2024-09-04T12:26:31Z")

</div>

@dominique What are the versions of Robot Framework and Browser Library that you are using?

[Next page](https://forum.robotframework.org/t/how-to-sort-6-or-more-columns-in-a-grid/7761.md?page=2)
