AnsweredAssumed Answered

Sort layers in TOC using arcpy

Question asked by yaron148 on Dec 31, 2015
Latest reply on Dec 31, 2015 by yaron148

Hi,

i try to sort layers in the TOC, in ascending or by name alphbetical with this code:

 

import arcpy

mxd = arcpy.mapping.MapDocument(r"D:\desktop\Project\project.mxd")
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")
    print name +' sorted'
   
arcpy.mapping.RemoveLayer(df, group_lyr)

and got:

 

>>> 
land use sorted
river sorted
>>> 

 

but the TOC do't sorted. For clarity, i saw this question in arcgis 10.0 - Arc GIS 10.1 - How to sort Layers ascending at TOC in "List by drawing order" - Geographic Information Sys…

I work with arc view 10.3. I know it possible with extension, but i have no extensions.

Is it possible to do it with python?

Outcomes