Sorting Dataset on column with numbers

Hey everybody,

So I have a dataset sorted off of lines. The key column has a name like “Line #” where # is anywhere between 1 and 20.

The problem I’ve been seeing is that alphabetically sorted datasets will return this:

Line | Value

Line 1 | 50
Line 10| 35
Line 11| 46
Line 2 | 20

Where I need:

Line | Value

Line 1 | 50
Line 2 | 20
Line 3 | 66
Line 4 | 75

Any way to do this? Thanks.

I’d bring the line number in as an integer. If you’re displaying this in a table, you can use the word “Line” as a prefix on the column.

Hi sthayer,

If you are using the dataset on a table then Jordan’s suggestion of using "Line " as a column prefix is the way to go. This is done in the Table Customizer on the table. So you set "Line " as the prefix for the column and you populate the dataset with integers instead of “Line #” – because the table will insert the "Line " part for you if you use it as a prefix.

If you want a flexible, powerful way to sort a dataset the following way works:

Here’s how to do it using the MutablePyDataSet from the PA Power Scripting Module:

#get the component that has the dataset
table = event.source.parent.getComponent('Table')
#convert the dataset into a MutablePyDataSet
data = pa.dataset.toData(
#sort the MutablePyDataSet
data.sortRows(key=lambda row: int(row["Line"].split(" ")[1]))
#convert it back into a Dataset and assign it to the table = data.toDataSet()

The sortRows method takes a function that is used to evaluate each row. The function returns a value to use to sort each row. lambda is Python syntax for creating an anonymous function on one line of code. The function that I passed in splits the “Line” column into a list on the space " " character, then it gets the second item in the list, which is where the number is, and converts the string number into an integer number, so that the integer number is used for sorting. You can see the documentation for the sortRows method here: … -sort-rows