# How do you sort a dataset

**URL:** <https://forum.inductiveautomation.com/t/how-do-you-sort-a-dataset/1467>\
**Category:** Archived Topics\
**Created:** [January 29, 2010, 6:29pm UTC](https://forum.inductiveautomation.com/t/how-do-you-sort-a-dataset/1467 "2010-01-29T18:29:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimE](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@TimE](https://forum.inductiveautomation.com/u/TimE)\
**Post date:** [January 29, 2010, 6:29pm UTC](https://forum.inductiveautomation.com/t/how-do-you-sort-a-dataset/1467/1 "2010-01-29T18:29:11Z")

</div>

Hi,  
I am collecting data from various Lines and Machines and storing it in multiple tables. I often want to display this data on a window using a table where the column headings of the database table are in one column of a dataset and the value is in the next column. Basically I want to turn the table on it’s side.

I have the following function in a module that I call when I need to do this. This is working very well but I would like to sort the column names.

I would like to either sort by column name at the query definition or within the function. Is there a simple way to do this?

`def turnTableSideways(ds):
	import fpmi
	headers = []
	for c in range(ds.getColumnCount()):
		headers.append(ds.getColumnName(c))
	pyNewData = []
	pyOldData = fpmi.dataset.toPyDataSet(ds)
	for x in range(len(headers)):
		strDetail = str(pyOldData[0][x])
		pyNewData.append([headers[x],strDetail])
	dsSideways = fpmi.dataset.toDataSet(["Item","Value"],pyNewData)
	return dsSideways`  
Thanks,

---

<div class="post-metadata">

**Author:** ![TimE](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@TimE](https://forum.inductiveautomation.com/u/TimE)\
**Post date:** [January 29, 2010, 6:35pm UTC](https://forum.inductiveautomation.com/t/how-do-you-sort-a-dataset/1467/2 "2010-01-29T18:35:05Z")

</div>

Sorry, I should have searched the net for this first.

I added pyNewData.sort() to the existing code.

`def turnTableSideways(ds):
   import fpmi
   headers = []
   for c in range(ds.getColumnCount()):
      headers.append(ds.getColumnName(c))
   pyNewData = []
   pyOldData = fpmi.dataset.toPyDataSet(ds)
   for x in range(len(headers)):
      strDetail = str(pyOldData[0][x])
      pyNewData.append([headers[x],strDetail])
   pyNewData.sort()
   dsSideways = fpmi.dataset.toDataSet(["Item","Value"],pyNewData)
   return dsSideways`

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [January 29, 2010, 7:20pm UTC](https://forum.inductiveautomation.com/t/how-do-you-sort-a-dataset/1467/3 "2010-01-29T19:20:21Z")

</div>

Glad you got it working.

For what its worth, I would like to add a rotateDataset() and sortDataset() function to the system.dataset library.
