I wanted to quickly create a kmz for a client which contained all of their asset data. It didn’t need to be in any order, but then I thought, why not alphabetical?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|# @date 03/07/2015|
|# @author Cindy Williams|
|# Sorts the TOC in ArcMap alphabetically using|
|# a temporary group layer for reference. This group|
|# layer should be empty, and can be created anywhere|
|# in the TOC.|
|# For use in the Python window in ArcMap.|
|mxd = arcpy.mapping.MapDocument("CURRENT")|
|df = arcpy.mapping.ListDataFrames(mxd) # Assuming one data frame|
|group_lyr = [lyr for lyr in arcpy.mapping.ListLayers(mxd) if lyr.isGroupLayer] # 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), "BEFORE")|
After trying out a few different ways, I decided to use an empty group layer as the anchor layer for sorting. One cannot add a group layer to the table of contents via Python, so unfortunately that step is a manual exercise.
This still beats manually moving dozens of layers up and down though 🙂