<?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: how to sort a ListLayers list? in Transportation Questions</title>
    <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742304#M2449</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Great solution Curtis. This also can be a time that the collections module can come in handy, it provides OrderedDict for cases where you want things sorted:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import collections
d = dict((x.name, x) for x in layers)
od = collections.OrderedDict(sorted(d.items()))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now od can be accessed sequentially and retains the relationship between name and layer.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 07:35:44 GMT</pubDate>
    <dc:creator>ShaunWalbridge</dc:creator>
    <dc:date>2021-12-12T07:35:44Z</dc:date>
    <item>
      <title>how to sort a ListLayers list?</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742294#M2439</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I get a list of layers from ArcMap. If I sort the layer list, it doesn't seem to sort. Here's a snippet of the code I use.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I attached a text file showing the results when I run the script from the python window in ArcMap. I also attached an image showing my table of contents with 219 layers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;When I use the code in a script, the results are a lot worst then the results show.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm using 10.1, sp1 on XP.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")
dfList = arcpy.mapping.ListDataFrames(mxd,"*")
df = dfList[0]
layerList = arcpy.mapping.ListLayers(mxd)
layerList.sort()
print str(len(layerList))
#print layerList
for layer in layerList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print layer.name&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:52:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742294#M2439</guid>
      <dc:creator>JasonMills</dc:creator>
      <dc:date>2021-12-12T16:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a ListLayers list?</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742295#M2440</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: JSkinn3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi Jason,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try appending the layer names to a list, and then sorting that list.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")
dfList = arcpy.mapping.ListDataFrames(mxd,"*")
df = dfList[0]
layerList = arcpy.mapping.ListLayers(mxd)
list = []
for layer in layerList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; list.append(layer.name)
list.sort()
for lyr in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:35:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742295#M2440</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T07:35:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a ListLayers list?</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742296#M2441</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi Jason,&lt;BR /&gt;&lt;BR /&gt;Try appending the layer names to a list, and then sorting that list.&amp;nbsp; Ex:&lt;BR /&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That sorted the list of names correctly. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;One of my scripts calculates a couple of fields in all of the mine layers. While it doesn't matter if the list is sorted or not, it would be nice to see where how far along the script is. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, why doesn't it sort correctly?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 21 Jun 2013 15:38:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742296#M2441</guid>
      <dc:creator>JasonMills</dc:creator>
      <dc:date>2013-06-21T15:38:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a ListLayers list?</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742297#M2442</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: curtvprice&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;why doesn't it sort correctly?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ListLayers() returns a list of layer objects, not just strings. Jake's excellent example showed how you extract the names into a list of strings so you can sort them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The distinction between the objects and generic strings is a bit subtle, as the layer object has a method under the hood associated with it that, when you convert it to a string (say "print lyr"), returns the layer name. And when tools are provided a string as a layer argument, the arcpy uses the string to locate the layer object, and passes the layer the tool for you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 21 Jun 2013 15:46:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742297#M2442</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-06-21T15:46:17Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a ListLayers list?</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742298#M2443</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;ListLayers() returns a list of layer objects, not just strings. Jake's excellent example showed how you extract the names into a list of strings so you can sort them.&lt;BR /&gt;&lt;BR /&gt;The distinction between the objects and generic strings is a bit subtle, as the object has code associated with it that when you convert it to a string (say "print lyr") it will display the layer name. And when tools are provided a string, as a layer argument, the system locates the layer object based on its name and converts it for you.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So, there's really not an easy way to sort the list? I need to process the layers, not the name.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 21 Jun 2013 16:07:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742298#M2443</guid>
      <dc:creator>JasonMills</dc:creator>
      <dc:date>2013-06-21T16:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a ListLayers list?</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742299#M2444</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: curtvprice&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;So, there's really not an easy way to sort the list? I need to process the layers, not the name.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You could do this using a bit of complex code, but you don't need to because in arcpy you access the layers by name. So a sorted list of name strings works, as I think you already discovered.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 21 Jun 2013 16:33:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742299#M2444</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-06-21T16:33:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a ListLayers list?</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742300#M2445</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You could do this using a bit of complex code, but you don't need to because in arcpy you access the layers by name. So a sorted list of name strings works, as I think you already discovered.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Yeah, the layer name works great. Unless I'm using the layer name in my script logic. And someone happens to change the layer name. And my script can't see that unless it looks at the dataset name. Which is in the layer object. Which is why I wanted to sort the list of layers and not just a list of names.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This is what I ended up with.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def sortedDictValues(adict):
&amp;nbsp;&amp;nbsp;&amp;nbsp; keys = adict.keys()
&amp;nbsp;&amp;nbsp;&amp;nbsp; keys.sort()
&amp;nbsp;&amp;nbsp;&amp;nbsp; return map(adict.get, keys)

mxd = arcpy.mapping.MapDocument("CURRENT")
dfList = arcpy.mapping.ListDataFrames(mxd,"*")
df = dfList[0]
layerList = arcpy.mapping.ListLayers(mxd)
layerDict = {}
print str(len(layerList))
#print layerList
for layer in layerList:
&amp;nbsp;&amp;nbsp; layerDict[layer.name] = layer
sortedList = sortedDictValues(layerDict)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:35:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742300#M2445</guid>
      <dc:creator>JasonMills</dc:creator>
      <dc:date>2021-12-12T07:35:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a list of objects by a property (like "name")</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742301#M2446</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: curtvprice&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Yeah, the layer name works great. Unless I'm using the layer name in my script logic. And someone happens to change the layer name. And my script can't see that unless it looks at the dataset name. Which is in the layer object. Which is why I wanted to sort the list of layers and not just a list of names.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Quite right. I found a neat and very fast way to do this, along those lines. Probably pretty equivalent to what you did using sortedDictValues:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;lyrNames = sorted([x.name for x in lyrs]) d = dict((x.name, x) for x in lyrs) lyrsSorted = [d&lt;I&gt; for i in lyrNames]&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 21 Jun 2013 17:21:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742301#M2446</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-06-21T17:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a list of objects by a property (like "name")</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742302#M2447</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Quite right. I found a neat and very fast way to do this, along those lines. Probably pretty equivalent to what you did using sortedDictValues:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
lyrNames = sorted([x.name for x in lyrs])
d = dict((x.name, x) for x in lyrs)
lyrsSorted = [d&lt;I&gt; for i in lyrNames]
&lt;/I&gt;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That's great! I like that better than having another function.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:35:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742302#M2447</guid>
      <dc:creator>JasonMills</dc:creator>
      <dc:date>2021-12-12T07:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a list of objects by a property (like "name")</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742303#M2448</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: curtvprice&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's an even better one, from &lt;/SPAN&gt;&lt;A href="http://stackoverflow.com/questions/403421/how-to-sort-a-list-of-objects-in-python-based-on-an-attribute-of-the-objects" rel="nofollow noopener noreferrer" target="_blank"&gt;stackoverflow&lt;/A&gt;&lt;SPAN&gt;. Finally an example of the use of lambda that makes sense to me!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# in place
lyrs.sort(key=lambda x: x.name, reverse=False)
# make a new list
lyrs_sort = sorted(lyrs, key=lambda x: x.name, reverse=False)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:35:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742303#M2448</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T07:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: how to sort a ListLayers list?</title>
      <link>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742304#M2449</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Great solution Curtis. This also can be a time that the collections module can come in handy, it provides OrderedDict for cases where you want things sorted:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import collections
d = dict((x.name, x) for x in layers)
od = collections.OrderedDict(sorted(d.items()))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now od can be accessed sequentially and retains the relationship between name and layer.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:35:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/how-to-sort-a-listlayers-list/m-p/742304#M2449</guid>
      <dc:creator>ShaunWalbridge</dc:creator>
      <dc:date>2021-12-12T07:35:44Z</dc:date>
    </item>
  </channel>
</rss>

