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

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?

Show your code. See Wiki - how to post code on this forum for instructions on formatting.

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

	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)

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.

	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

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

	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	

Seems to work just as expected, thank you!