# \[Bug\] sortDataset Expression Function Odd Alphanumeric Sort Order

**URL:** <https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096>\
**Category:** Ignition\
**Tags:** ignition79, ignition80\
**Created:** [April 21, 2020, 9:57pm UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096 "2020-04-21T21:57:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 21, 2020, 9:57pm UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096/1 "2020-04-21T21:57:57Z")

</div>

I think this is a bug but I am not certain. In the vision module, the sortDataset() expression handles strings that contain letters and numbers a little odd. It will sort character by character for any letters, which is expected, and I would expect this same behavior for every character in the string, regardless of whether or not it is a letter. However, it appears that, any sequence of numbers within the string are treated as a single number for the character position at the start of the numeric sequence.

**Example:**  
Unsorted dataset (one string type column):

1. A33Z
2. A8F3
3. A1ZZ

Result of sortDataset Expression (ascending argument = True):

1. A1ZZ
2. A8F3
3. A33Z (because 33 \> 8?)

I would expect (and the python list.sort() method matches this):

1. A1ZZ
2. A33Z
3. A8F3

I see the same results on 8.0.10 (Linux) and 7.9.12 (Windows).

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 21, 2020, 10:33pm UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096/2 "2020-04-21T22:33:44Z")

</div>

Looks like it intentionally uses a more “friendly” alphanumeric sorting method when sorting on strings. Starting in… Ignition 7.8.3.

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 21, 2020, 10:43pm UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096/3 "2020-04-21T22:43:08Z")

</div>

> [@Kevin.Herron](#):
>
> “friendly”

Ha, perfect description!

I could see where the current behavior would be useful ('PLC\_1', 'PLC\_8', 'PLC\_100'). If it is intentional, then it looks like the user manual needs a revision (unless I'm reading it wrong). Would be great, if this is intentional, if we could add in an optional flag to change the sort behavior to a true character by character sort.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 21, 2020, 10:44pm UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096/4 "2020-04-21T22:44:03Z")

</div>

I can bring it up, but the original ticket from 2016 that implemented this explicitly ruled out the option of making it configurable.

For now, roll your own like before we had this scripting method: [How do you sort a dataset?](http://forum.inductiveautomation.com/t/how-do-you-sort-a-dataset/4090/4?u=kevin.herron)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 21, 2020, 10:51pm UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096/5 "2020-04-21T22:51:28Z")

</div>

Ooph, I just realized this was about the expression function, not scripting function. It uses the same utility method with the “friendly” sort order, but rolling your own would mean relying on `runScript` + my previous post.

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 21, 2020, 10:59pm UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096/6 "2020-04-21T22:59:01Z")

</div>

> [@Kevin.Herron](#):
>
> original ticket from 2016 that implemented this explicitly ruled out the option of making it configurable.

Bummer. Well I guess I would change my feature request to creating a second sortDataset function with the character by character sort 😅

Yeah, we came up with a workaround that is very similar to that post and using the runscript() function. The simplicity of the sortDataset function was nice...makes things a lot cleaner.

So I guess the only bug here is in the user manual (for both the expression and scripting functions):

> Sorts a dataset and returns the sorted dataset. This works on numeric, as well as alphanumeric columns. It will go character by character, going from 0-9, A-Z, a-z.

---

<div class="post-metadata">

**Author:** ![Paul.Scott](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul.scott/32/4578_2.png) [@Paul.Scott](https://forum.inductiveautomation.com/u/Paul.Scott)\
**Post date:** [April 21, 2020, 11:47pm UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096/7 "2020-04-21T23:47:36Z")

</div>

> [@WillMT10](#):
>
> So I guess the only bug here is in the documentation (for both the expression and scripting functions):

Yikes! That's embarrassing. Sorry about the mix-up there. We'll have the documentation team make the corrections.

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 22, 2020, 12:18am UTC](https://forum.inductiveautomation.com/t/bug-sortdataset-expression-function-odd-alphanumeric-sort-order/34096/8 "2020-04-22T00:18:29Z")

</div>

> [@Kevin.Herron](#):
>
> For now, roll your own like before we had this scripting method: [How do you sort a dataset?](http://forum.inductiveautomation.com/t/how-do-you-sort-a-dataset/4090/4)

Just in case someone else only needs to sort by one column in the dataset you can use this smaller piece of code:

```auto
def sortDataset(ds, sort_col=0, reverse_order=False):
	hdrs = system.dataset.getColumnHeaders(ds)
	py_ds = system.dataset.toPyDataSet(ds)
	ds_array = [[col for col in row] for row in py_ds]
	ds_array.sort(key=lambda x: x[sort_col], reverse=reverse_order)
	
	return system.dataset.toDataSet(hdrs, ds_array)

```
