<?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: Sorting layers in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sorting-layers/m-p/1495468#M70875</link>
    <description>&lt;P&gt;It wasn't an option as I wanted to automate this, the script worked thank you so much!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Jun 2024 16:09:40 GMT</pubDate>
    <dc:creator>HusamOqer</dc:creator>
    <dc:date>2024-06-20T16:09:40Z</dc:date>
    <item>
      <title>Sorting layers</title>
      <link>https://community.esri.com/t5/python-questions/sorting-layers/m-p/1495106#M70861</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am working on a script that plot many layers to an aprx file and do some other stuff and I would like to sort those layers based on the naming (Descending or Ascending order).&lt;/P&gt;&lt;P&gt;Any recommendation on how to do that using arcpy for GIS Pro?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 21:02:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sorting-layers/m-p/1495106#M70861</guid>
      <dc:creator>HusamOqer</dc:creator>
      <dc:date>2024-06-19T21:02:56Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting layers</title>
      <link>https://community.esri.com/t5/python-questions/sorting-layers/m-p/1495334#M70868</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HaydenWelch_0-1718889849895.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/107507iDDD2C614BA090850/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HaydenWelch_0-1718889849895.png" alt="HaydenWelch_0-1718889849895.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Is this not an option? If you need to automate it, you can use the mapping module.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

project = arcpy.mp.ArcGISProject("CURRENT")

for mp in project.listMaps():
    # Sort Layers by Name
    sorted_layers = sorted(mp.listLayers(), key=lambda lyr: lyr.name)
    previous_layer = None
    
    # Iterate through sorted layers and move them to the top of the map
    for lyr in sorted_layers:
        # Use existing first layer as reference for sorted first layer
        if not previous_layer:
            mp.moveLayer(mp.listLayers()[0], lyr, "BEFORE")
        else:
            mp.moveLayer(previous_layer, lyr, "AFTER")
        previous_layer = lyr&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have group layers and want to preserve internal group layer ordering, you will have to update the lyr.name sory key to lyr.longName so the group layer name is included in the sort.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2024 13:37:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sorting-layers/m-p/1495334#M70868</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2024-06-20T13:37:50Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting layers</title>
      <link>https://community.esri.com/t5/python-questions/sorting-layers/m-p/1495468#M70875</link>
      <description>&lt;P&gt;It wasn't an option as I wanted to automate this, the script worked thank you so much!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2024 16:09:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sorting-layers/m-p/1495468#M70875</guid>
      <dc:creator>HusamOqer</dc:creator>
      <dc:date>2024-06-20T16:09:40Z</dc:date>
    </item>
  </channel>
</rss>

