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: print layer.name
Solved! Go to Solution.
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.
lyrNames = sorted([x.name for x in lyrs]) d = dict((x.name, x) for x in lyrs) lyrsSorted = [d for i in lyrNames]
mxd = arcpy.mapping.MapDocument("CURRENT") dfList = arcpy.mapping.ListDataFrames(mxd,"*") df = dfList[0] layerList = arcpy.mapping.ListLayers(mxd) list = [] for layer in layerList: list.append(layer.name) list.sort() for lyr in list: print lyr
Hi Jason,
Try appending the layer names to a list, and then sorting that list. Ex:
why doesn't it sort correctly?
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.
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.
So, there's really not an easy way to sort the list? I need to process the layers, not the name.
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.
def sortedDictValues(adict): keys = adict.keys() keys.sort() 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: layerDict[layer.name] = layer sortedList = sortedDictValues(layerDict)
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.
lyrNames = sorted([x.name for x in lyrs]) d = dict((x.name, x) for x in lyrs) lyrsSorted = [d for i in lyrNames]
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:lyrNames = sorted([x.name for x in lyrs]) d = dict((x.name, x) for x in lyrs) lyrsSorted = [d for i in lyrNames]
# 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)