# Power Table Sort Script

**URL:** <https://forum.inductiveautomation.com/t/power-table-sort-script/15524>\
**Category:** Ignition\
**Created:** [September 30, 2017, 7:51pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524 "2017-09-30T19:51:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![weertske](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/weertske/32/3353_2.png) [@weertske](https://forum.inductiveautomation.com/u/weertske)\
**Post date:** [September 30, 2017, 7:51pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/1 "2017-09-30T19:51:53Z")

</div>

For those of you who would like to know how to trigger a Power Table to sort via script, here is how you do it:

table = event.source.parent.getComponent(‘Table’)  
gTable = table.getTable()  
gTable.setAutoCreateRowSorter(True)  
gTable.getRowSorter().toggleSortOrder(0) # the 0 (zero) sets which column to sort on

---

<div class="post-metadata">

**Author:** ![ghuck](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ghuck/32/8337_2.png) [@ghuck](https://forum.inductiveautomation.com/u/ghuck)\
**Post date:** [October 4, 2017, 11:08pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/2 "2017-10-04T23:08:56Z")

</div>

When I did something like this to turn off the filters, I found I had a memory leak. Not sure if this will apply to the sorter as well but you might need to deallocate the sorter at the end with something like;  
gTable.setAutoCreateRowSorter(False)

---

<div class="post-metadata">

**Author:** ![Kleppe85](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@Kleppe85](https://forum.inductiveautomation.com/u/Kleppe85)\
**Post date:** [November 9, 2017, 10:44pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/3 "2017-11-09T22:44:56Z")

</div>

Spent some time on this today and figured out another way to sort the Power table.  
Currently running this script on Power Table “init” extension function.

```
from javax.swing import SortOrder
from javax.swing import RowSorter

sortList = []
sortList.append(RowSorter.SortKey(0, SortOrder.DESCENDING)) #0 equals the column number, SortOrder either "ASCENDING" or "DESCENDING".
table = self
gTable = table.getTable()
gTable.setAutoCreateRowSorter(True)
gTable.getRowSorter().setSortKeys(sortList)
gTable.setAutoCreateRowSorter(False)
```

---

<div class="post-metadata">

**Author:** ![c.turmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/c.turmel/32/67745_2.png) [@c.turmel](https://forum.inductiveautomation.com/u/c.turmel)\
**Post date:** [January 28, 2020, 2:04pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/4 "2020-01-28T14:04:30Z")

</div>

table = self  
gTable = table.getTable()  
gTable.setAutoCreateRowSorter(True)  
gTable.getRowSorter().toggleSortOrder(0) # the 0 (zero) sets which column to sort on

I added ‘‘Self’’ in the first line and works as a charm!

---

<div class="post-metadata">

**Author:** ![hachjessed](https://avatars.discourse-cdn.com/v4/letter/h/7ab992/32.png) [@hachjessed](https://forum.inductiveautomation.com/u/hachjessed)\
**Post date:** [August 26, 2020, 10:14pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/5 "2020-08-26T22:14:57Z")

</div>

Anyone know how to apply **setRowFilter** to a power table?

---

<div class="post-metadata">

**Author:** ![francois.morin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/francois.morin/32/15064_2.png) [@francois.morin](https://forum.inductiveautomation.com/u/francois.morin)\
**Post date:** [September 24, 2020, 12:27pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/6 "2020-09-24T12:27:16Z")

</div>

I tried both your sample code and it works fine…  
Until you have custom formating in “configureCell” event.

The event “configureCell” is not triggered after applying the sort that way.  
Anybody have a solution to force the trigger of “configureCell” event ?

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [September 24, 2020, 1:04pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/7 "2020-09-24T13:04:11Z")

</div>

I just load the data in a different, custom property. Sort the dataset with a script, and put it on the table.

---

<div class="post-metadata">

**Author:** ![francois.morin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/francois.morin/32/15064_2.png) [@francois.morin](https://forum.inductiveautomation.com/u/francois.morin)\
**Post date:** [October 2, 2020, 3:17pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/8 "2020-10-02T15:17:10Z")

</div>

I also did that in the end.

I liked the way the other option was showing which column was sorted, which I do not get with the current solution.  
I tried to implement both but it is not working very well.

---

<div class="post-metadata">

**Author:** ![edward.fowler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/edward.fowler/32/24179_2.png) [@edward.fowler](https://forum.inductiveautomation.com/u/edward.fowler)\
**Post date:** [June 3, 2021, 1:39pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/9 "2021-06-03T13:39:05Z")

</div>

@Sanderd17  
Are you doing something like this? I’m trying to sort by script and am testing with a ‘mouse pressed’ event handler script.

```
unsortedData = event.source.data
sortedData = system.dataset.sort(unsortedData, 1, False)
event.source.data = sortedData

```

It’s not working…the table doesn’t change.

Much appreciated,  
Ted.

---

<div class="post-metadata">

**Author:** ![edward.fowler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/edward.fowler/32/24179_2.png) [@edward.fowler](https://forum.inductiveautomation.com/u/edward.fowler)\
**Post date:** [June 3, 2021, 2:55pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/10 "2021-06-03T14:55:37Z")

</div>

I solved the problem, perhaps not but I got it to do what I want. I had bound my client dataset tag to the table and got to thinking…if I get the from the table and sort it and set the data in the table to the new dataset created from the sorting, it probably goes into the table but is overwritten the to tag dataset because of the binding. I removed the dataset tag binding from the table then added a window-open script that reads the dataset from the tag, creates a new data set via sorting that set the table data to the sorted dataset. Bingo Gingo, it shows my sorted data.

table = system.gui.getParentWindow(event).getComponentForPath(‘mwVersionInfo.Power Table’)  
readList = system.tag.readBlocking(’[gw1]\_Project Version Information/versionDataSet’)  
unsortedData = readList[0].value  
sortedData = system.dataset.sort(unsortedData, 0, False)  
table.data = sortedData

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 3, 2021, 3:01pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/11 "2021-06-03T15:01:20Z")

</div>

It is easier and more reliable to just add a custom dataset property to your table with the original binding. A propertyChange event that notes the change to that property would then sort and write to `.data`.

---

<div class="post-metadata">

**Author:** ![edward.fowler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/edward.fowler/32/24179_2.png) [@edward.fowler](https://forum.inductiveautomation.com/u/edward.fowler)\
**Post date:** [June 3, 2021, 3:28pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/12 "2021-06-03T15:28:34Z")

</div>

@pturmel Hello Phil.

I tried that; added a custom dataset property to the table and bound it to the tag with the dataset and added a project change event to the table to sort the data set.

I’m sure I started an infinite loop…sort the data it detected and change and sorted the data and detected… I didn’t note any specific property changing…what should I be noting? All I’m trying to do is sort the data each time the window opens.

Thank you and regards,  
Ted.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 3, 2021, 5:28pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/13 "2021-06-03T17:28:05Z")

</div>

Did your propertyChange event script check event.propertyName? That’s a common cause of infinite event loops. It should look something like this:

```auto
if event.propertyName=='MyCustomDataProp':
	event.source.data = system.dataset.sort(event.newValue, 0, False)

```

If you don’t check the property name, then your script will be flooded with every possible change, including recursion when you are assigning back into the same component.

_ **Always check the property name in Vision propertyChange events.** _

---

<div class="post-metadata">

**Author:** ![edward.fowler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/edward.fowler/32/24179_2.png) [@edward.fowler](https://forum.inductiveautomation.com/u/edward.fowler)\
**Post date:** [June 4, 2021, 1:39pm UTC](https://forum.inductiveautomation.com/t/power-table-sort-script/15524/14 "2021-06-04T13:39:28Z")

</div>

@pturmel Hi Phil, thank you!  
I get it now, and it works like a charm!  
Thanks again for all your help.  
Regards,  
Ted.
