# Sort Order of Columns in table.filter.results.data

**URL:** <https://forum.inductiveautomation.com/t/sort-order-of-columns-in-table-filter-results-data/89126>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [April 19, 2024, 4:55pm UTC](https://forum.inductiveautomation.com/t/sort-order-of-columns-in-table-filter-results-data/89126 "2024-04-19T16:55:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_Quinn](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@Colin\_Quinn](https://forum.inductiveautomation.com/u/Colin_Quinn)\
**Post date:** [April 19, 2024, 4:55pm UTC](https://forum.inductiveautomation.com/t/sort-order-of-columns-in-table-filter-results-data/89126/1 "2024-04-19T16:55:17Z")

</div>

Hi, I am working on an export feature for a table with filters enabled. When exporting the entire table I am able to just use the table's column order, however when using the filter I am unable to control the order in which the data is organized.

I have tried converting the filtered data into a dataset and using the `system.dataset.filterColumns()` function with the list of headers in the order I want and that does work for the columns, but not the associated values. Is there something I'm missing?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 19, 2024, 5:06pm UTC](https://forum.inductiveautomation.com/t/sort-order-of-columns-in-table-filter-results-data/89126/2 "2024-04-19T17:06:46Z")

</div>

Show your code. See [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831) for instructions on formatting.

---

<div class="post-metadata">

**Author:** ![Colin\_Quinn](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@Colin\_Quinn](https://forum.inductiveautomation.com/u/Colin_Quinn)\
**Post date:** [April 19, 2024, 5:12pm UTC](https://forum.inductiveautomation.com/t/sort-order-of-columns-in-table-filter-results-data/89126/3 "2024-04-19T17:12:23Z")

</div>

The following is on the `onClick` action for the export button.

```python
	headers = [
		'header_1',
        'header_2',
        'header_3',
        'header_4',
       # ... and so on ...
	]
	if self.parent.parent.parent.getChild("Table").props.filter.results.data:
		value = self.parent.parent.parent.getChild("Table").props.filter.results.data
		data = []
	# TODO: Sort the order of the columns and values	
        for row in value:
			data.append([val for key,val in row.iteritems()])
		
		data = system.dataset.toDataSet(headers,data)
	else:
		data = self.parent.parent.parent.getChild("Table").props.data
	
	# Create the Excel bytes
	spreadsheet = system.dataset.toExcel(True, [data])
    
    # Define the filename
	filename = "Export.xlsx"	
	# Trigger download
	system.perspective.download(filename, spreadsheet)

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 19, 2024, 5:48pm UTC](https://forum.inductiveautomation.com/t/sort-order-of-columns-in-table-filter-results-data/89126/4 "2024-04-19T17:48:29Z")

</div>

Dictionaries in Jython (based on Python 2.7) can't be ordered.  
I created a custom property, `filteredData` on the default table and add the modified version of your code below to a button.

```python
	headers = [
		'city',
        'country',
        'population'
	]
	if self.parent.getChild("Table").props.filter.results.data:
		value = self.parent.getChild("Table").props.filter.results.data
		data = []
		# TODO: Sort the order of the columns and values	
		for row in value:
			data.append([val for key,val in row.iteritems()])
		
		data = system.dataset.toDataSet(headers,data)
	else:
		data = self.parent.parent.parent.getChild("Table").props.data
	
	self.getSibling("Table").custom.filteredData = data	

```

When I filter on "united" and click the button

```python
"#NAMES"
"city","country","population"
"#TYPES"
"str","str","L"
"#ROWS","9"
"United States","Folsom","77271"
"United States","San Diego","1406630"
"United States","San Francisco","884363"
"United States","Washington, DC","658893"
"United Kingdom","London","8825001"
"United States","New York City","8622698"
"United States","Los Angeles","3884307"
"United States","Chicago","2695598"
"United States","Dallas","1317929"

```

So the problem is as you describe. The data produced has columns out of order.

The following code (and I'm no Python expert) seems to do the job for when there's filtered data. You'll need to add in for the unfiltered data case.

```python
	headers = [
		'city',
        'country',
        'population'
	]
	if self.parent.getChild("Table").props.filter.results.data:
		value = self.parent.getChild("Table").props.filter.results.data
		data = []
		for row in value:
			fields = []
			for h in headers:
				fields.append(row[h])
			data.append(fields)
		data = system.dataset.toDataSet(headers,data)
	else:
		data = self.parent.parent.parent.getChild("Table").props.data
	
	self.getSibling("Table").custom.filteredData = data	

```

---

<div class="post-metadata">

**Author:** ![Colin\_Quinn](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@Colin\_Quinn](https://forum.inductiveautomation.com/u/Colin_Quinn)\
**Post date:** [April 19, 2024, 6:16pm UTC](https://forum.inductiveautomation.com/t/sort-order-of-columns-in-table-filter-results-data/89126/5 "2024-04-19T18:16:56Z")

</div>

Seems to work just as expected, thank you!
