Select to view content in your preferred language

Alphabetically sorting Tables in the Contents Pane

220
2
Jump to solution
05-09-2024 11:28 AM
BrennanSmith1
Occasional Contributor

I know that you can sort layers in the table of contents, but this approach doesn't sort tables.  I have lots of standalone tables, and if they get knocked out of order for any reason, it is very time consuming to drag and drop to reorder them.  The following script seems to do the trick, and sorts all standalone tables in a new group in the TOC.  I'm posting here to help anyone who has a similar problem, and to see if anyone else has a better approach.

# get active map in current project
p = arcpy.mp.ArcGISProject("CURRENT")
m = p.activeMap

# sort table names
sortedtables = sorted([t.name for t in m.listTables()])

# create new group
g = m.createGroupLayer("Sorted Tables")

# for each table, add to group and remove the original
for x in sortedtables:
    t = m.listTables(x)[0]
    m.addTableToGroup(g,t)
    m.removeTable(t)

 

1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Thank for posting @BrennanSmith1 

Here is an alternative, not a perfect alternative but another option using the CIM.

This will not update while the APRX is open, if you run in an open CURRENT project you will need to close and reopen. If you run in a standalone script, one you reopen the APRX the layers are sorted.

import arcpy

## access the aPRX
aprx_path = "path/to/your.aprx"
aprx = arcpy.mp.ArcGISProject(aprx_path)

## access the Map
m = aprx.listMaps("Map")[0]

## get the CIM definition
m_cim = m.getDefinition("V3")

## current order
##for tbl in m_cim.standaloneTables:
##    print(tbl)

## sort
m_cim.standaloneTables = sorted(m_cim.standaloneTables)

## set new definition
m.setDefinition(m_cim)

## open aprx and see the change.

 

~ learn.finaldraftmapping.com

View solution in original post

2 Replies
Clubdebambos
Occasional Contributor III

Thank for posting @BrennanSmith1 

Here is an alternative, not a perfect alternative but another option using the CIM.

This will not update while the APRX is open, if you run in an open CURRENT project you will need to close and reopen. If you run in a standalone script, one you reopen the APRX the layers are sorted.

import arcpy

## access the aPRX
aprx_path = "path/to/your.aprx"
aprx = arcpy.mp.ArcGISProject(aprx_path)

## access the Map
m = aprx.listMaps("Map")[0]

## get the CIM definition
m_cim = m.getDefinition("V3")

## current order
##for tbl in m_cim.standaloneTables:
##    print(tbl)

## sort
m_cim.standaloneTables = sorted(m_cim.standaloneTables)

## set new definition
m.setDefinition(m_cim)

## open aprx and see the change.

 

~ learn.finaldraftmapping.com
BrennanSmith1
Occasional Contributor

Thank you for sharing -  I think this approach will be much faster for a large number of tables, since it doesn't require adding and deleting.  Two things to be aware of with the CIM approach - it will not recognize tables already in a group (only standaloneTables, like the function says), and it doesn't recognize hosted feature layer table names (it sees itemID.json)