# TagBrowse Sorts Characters not numbers

**URL:** <https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [November 5, 2025, 9:55pm UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975 "2025-11-05T21:55:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tebright1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tebright1/32/87405_2.png) [@tebright1](https://forum.inductiveautomation.com/u/tebright1)\
**Post date:** [November 5, 2025, 9:55pm UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975/1 "2025-11-05T21:55:26Z")

</div>

I have a script that was working great sorting tags until I added one with a label over 99.

I am using system.tag.browsTags to sort through some tags and generate a few JSON objects.

It looks to sort by character and not number. Can you change the sort outside of ASC or DESC?

I could break the sort into the first 99, then do another sort for larger values. Hoping to find a quicker solution. Thanks!!

Code:

```python
tagPath = self.session.custom.TagPath
tagList = system.tag.browseTags(parentPath=tagPath,udtParentType='Sensor',sort='ASC') 
return tagList

```

Sort causes 100 to follow 10. Sensor10, Sensor100, etc.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/0/f01ec1b624b20b455a74177626ca4e3ec52eb631.png)

---

<div class="post-metadata">

**Author:** ![David\_Stone](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/david_stone/32/79749_2.png) [@David\_Stone](https://forum.inductiveautomation.com/u/David_Stone)\
**Post date:** [November 5, 2025, 10:10pm UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975/2 "2025-11-05T22:10:09Z")

</div>

You should be consistent with your tag naming.

`[Ignition_monterey_in_edge]Indiana/Monterey/LoehmerDairy/Sensor06`

This should be padded correctly for the number of tags you are using:

`[Ignition_monterey_in_edge]Indiana/Monterey/LoehmerDairy/Sensor006`

If you use this consistency in naming, it will sort correctly. The sorting always runs left to right, character by character, so `Sensor10` and `Sensor100` are identical up to character 8, then 100 has an extra character and will be placed after `Sensor10` but before `Sensor11`

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 5, 2025, 10:41pm UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975/3 "2025-11-05T22:41:52Z")

</div>

Here's a general purpose "human" sort operation (instead of lexicographic) you can put in your project library:

```python
from com.inductiveautomation.ignition.common.util import Comparators

__comparator = Comparators.alphaNumeric()

def alphaSort(iterable, key, reverse=False):
	return sorted(
		tags, 
		cmp=lambda n1, n2: __comparator.compare(n1, n2), 
		key=key,
		reverse=reverse,
	)

```

You'll have to pass a `key` function (that itself provides a string to the inner comparator), much like the `sorted` builtin function expects, e.g:

```python
tags = system.tag.browseTags("")

for t in tags:
	print t.name
	

print

for t in alphaSort(tags, lambda t: t.name):
	print t.name

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 5, 2025, 11:44pm UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975/4 "2025-11-05T23:44:03Z")

</div>

If you've installed my Integration Toolkit (and why would you not?), you can use my [naturalOrder()](https://www.automation-pros.com/toolkit/doc/iteration.html#naturalorder) expression function with my [orderBy()](https://www.automation-pros.com/toolkit/doc/iteration.html#orderby) expresion function, or the script versions.

---

<div class="post-metadata">

**Author:** ![tebright1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tebright1/32/87405_2.png) [@tebright1](https://forum.inductiveautomation.com/u/tebright1)\
**Post date:** [November 6, 2025, 2:29am UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975/5 "2025-11-06T02:29:56Z")

</div>

Initially we were only using 01 to 99. Of course the number began to grow. The system is deployed and collecting historical data. Adjusting the tag name structure at this time wouldn't be ideal.

Thanks for the insight on the sorting.

---

<div class="post-metadata">

**Author:** ![tebright1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tebright1/32/87405_2.png) [@tebright1](https://forum.inductiveautomation.com/u/tebright1)\
**Post date:** [November 6, 2025, 2:30am UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975/6 "2025-11-06T02:30:38Z")

</div>

Awesome. Will give this a try. Thanks!

---

<div class="post-metadata">

**Author:** ![tebright1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tebright1/32/87405_2.png) [@tebright1](https://forum.inductiveautomation.com/u/tebright1)\
**Post date:** [November 6, 2025, 2:31am UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975/7 "2025-11-06T02:31:19Z")

</div>

Natural order is what I need. Will check out the toolkit. 🤩

---

<div class="post-metadata">

**Author:** ![tebright1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tebright1/32/87405_2.png) [@tebright1](https://forum.inductiveautomation.com/u/tebright1)\
**Post date:** [November 6, 2025, 2:20pm UTC](https://forum.inductiveautomation.com/t/tagbrowse-sorts-characters-not-numbers/111975/8 "2025-11-06T14:20:14Z")

</div>

Made one change. This worked great!

> [@paul-griffith](#):
>
> ```python
> from com.inductiveautomation.ignition.common.util import Comparators
> 
> __comparator = Comparators.alphaNumeric()
> 
> def alphaSort(iterable, key, reverse=False):
> return sorted(
> iterable, 
> cmp=lambda n1, n2: __comparator.compare(n1, n2), 
> key=key,
> reverse=reverse,
> )
> 
> ```
