how to sort a table by a column?

I am running an applescript that runs a query and opens results in a window with a tableview and columns that can be manually sorted by clicking on the columnheader in an ascending/descending order
I need the sorting to be done automatically within the script. How do I do that? Thank you.

What application are you using to display the data?