<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Alphabetically sorting Tables in the Contents Pane in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/alphabetically-sorting-tables-in-the-contents-pane/m-p/1460014#M70578</link>
    <description>&lt;P&gt;Thank you for sharing -&amp;nbsp; I think this approach will be much faster for a large number of tables, since it doesn't require adding and deleting.&amp;nbsp; 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)&lt;/P&gt;</description>
    <pubDate>Mon, 13 May 2024 18:54:56 GMT</pubDate>
    <dc:creator>BrennanSmith1</dc:creator>
    <dc:date>2024-05-13T18:54:56Z</dc:date>
    <item>
      <title>Alphabetically sorting Tables in the Contents Pane</title>
      <link>https://community.esri.com/t5/python-questions/alphabetically-sorting-tables-in-the-contents-pane/m-p/1424212#M70553</link>
      <description>&lt;P&gt;I know that you can sort&amp;nbsp;&lt;EM&gt;layers&lt;/EM&gt; in the table of contents, but this approach doesn't sort&amp;nbsp;&lt;EM&gt;tables&lt;/EM&gt;.&amp;nbsp; 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.&amp;nbsp; The following script seems to do the trick, and sorts all standalone tables in a new group in the TOC.&amp;nbsp; I'm posting here to help anyone who has a similar problem, and to see if anyone else has a better approach.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2024 18:28:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alphabetically-sorting-tables-in-the-contents-pane/m-p/1424212#M70553</guid>
      <dc:creator>BrennanSmith1</dc:creator>
      <dc:date>2024-05-09T18:28:30Z</dc:date>
    </item>
    <item>
      <title>Re: Alphabetically sorting Tables in the Contents Pane</title>
      <link>https://community.esri.com/t5/python-questions/alphabetically-sorting-tables-in-the-contents-pane/m-p/1447986#M70565</link>
      <description>&lt;P&gt;Thank for posting&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/39515"&gt;@BrennanSmith1&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an alternative, not a perfect alternative but another option using the &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm" target="_blank" rel="noopener"&gt;CIM&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 May 2024 10:28:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alphabetically-sorting-tables-in-the-contents-pane/m-p/1447986#M70565</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2024-05-12T10:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: Alphabetically sorting Tables in the Contents Pane</title>
      <link>https://community.esri.com/t5/python-questions/alphabetically-sorting-tables-in-the-contents-pane/m-p/1460014#M70578</link>
      <description>&lt;P&gt;Thank you for sharing -&amp;nbsp; I think this approach will be much faster for a large number of tables, since it doesn't require adding and deleting.&amp;nbsp; 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)&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 18:54:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/alphabetically-sorting-tables-in-the-contents-pane/m-p/1460014#M70578</guid>
      <dc:creator>BrennanSmith1</dc:creator>
      <dc:date>2024-05-13T18:54:56Z</dc:date>
    </item>
  </channel>
</rss>

