I have to programmatically sort several layers in an .mxd project that are each placed in the same group layer. I have found a script that partially works by sorting all layers in the TOC (Arc GIS 10.1 - How to sort Layers ascending at TOC in "List by drawing order").

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0] # Assuming one data frame
group_lyr = [lyr for lyr in arcpy.mapping.ListLayers(mxd) if lyr.isGroupLayer][0] # The temp group layer should be the only one
lyr_names = sorted(lyr.name for lyr in arcpy.mapping.ListLayers(mxd) if lyr.isFeatureLayer)

for name in lyr_names:
    arcpy.mapping.MoveLayer(df, group_lyr, arcpy.mapping.ListLayers(mxd, name)[0], "BEFORE")


However I am having trouble to apply this to a specific group layer (in this case 'GroupLayer1') as indicated in the illustration below.

Is there a solution that allows for sorting only the layers in a specific group layer?

enter image description here

  • Does this help? community.esri.com/thread/170316
    – a11
    Commented Oct 28, 2018 at 17:51
  • this is exactly the same code as I am using...
    – redshoe
    Commented Oct 28, 2018 at 17:58
  • did you save as suggested in comments in the link?
    – a11
    Commented Oct 28, 2018 at 18:41
  • Thank you for trying to help me, but saving the .mxd doesn't help in this case as the sorting of the layers is working (as illustrated in the middle part of the graph), but I can not do the sorting for the layer files that are in a specific group layer (GroupLayer1).
    – redshoe
    Commented Oct 28, 2018 at 19:58

1 Answer 1


Your code is flawed in that it is not referencing layers in the group layer and for some reason your last line is to delete the group layer!

Below is some code that correctly sorts and moves based upon an updating reference layer object.

import arcpy

# Get map document and it's first data frame
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

# Get first group layer
group_lyr = [lyr for lyr in arcpy.mapping.ListLayers(mxd) if lyr.isGroupLayer][0]

# Create a sorted list of layer names (strings) in the group layer
lyr_names = sorted(lyr.name for lyr in arcpy.mapping.ListLayers(group_lyr) if lyr.isFeatureLayer)

# Get the layer object from the group layer that is the first in the sorted list
ref_lyr = arcpy.mapping.ListLayers(group_lyr, lyr_names[0])[0]

# Report variables to ensure things are right.
print("*** Group layer")
print("*** Reference layer")
print("*** To sort")

# Main moving layer loop
for name in lyr_names:
    # Move a layer if it is NOT the reference layer
    if name != ref_lyr.name:
        arcpy.mapping.MoveLayer(df, ref_lyr, arcpy.mapping.ListLayers(group_lyr, name)[0], "AFTER")
        # Update reference layer to be the layer just moved to ensure correct sorting
        ref_lyr = arcpy.mapping.ListLayers(group_lyr, name)[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.