Sorting in Attribute Table

1780
2
05-12-2014 11:09 AM
New Contributor
Hi there,

I have a question about Sorting in Arc Map.
When I use ???Sort Ascending??? tool in Attribute Table, it sorts like below:
A1
A10
A100
A2
A20
A200
However, I would like to find out if there is a way to sort my field info so it can sort like
A1
A2
A10
A20
A100
A200

Thank you.
Tags (2)
2 Replies
by
Regular Contributor III
Not to my knowledge. This would be a text field, where characters are evaluated independently. Here, 2 will always come after 1 in the same position in the text. Maybe there's some font encoding trick I don't know, or maybe you could sort on a different field, say datecreated, that might achieve the same effect, but that depends on what's in your data.

One possible way to achieve this would be to add a new integer field, calculate it in python to int(field[1:]), which would give you a numeric field with just the number portion of your original field. Then sort on that. This won't work if you later have values like B1, B10, B20, B200... because it won't distinguish between A200 and B200, for example. But if all your values begin with A, should work.
MVP Honored Contributor
Another option that would not involve another field is to insert leading zeros or spaces into the numeric portion of the text after the letter to right justify the numbers.  As long as only one letter is at the front it would sort correctly for all values (A, B, etc.).  You would have to determine the maximum number of digits you would expect for these numeric values to make that work well for a long period of time.  The process could be reversed by a label expression that stripped out the leading zeros or spaces.

To calculate the field to handle 4 numeric characters (up to 9999) use something like:

field[:1] + field[1:].zfill(4)

And to reverse the process in a label expression use something like:

field[:1] + str(int(field[1:]))