Hi there, using arcpro deaktop
In my table of contents i have a group with many layers, how can i sort them by alphabetical order?
I can only find how to sort all table of contents but no explenation how to do that for only on group.
Thanks,
Tal
Happy to help! I get it now, we don't want to recursively sort inside the child folders, just their names. This should do it:
import arcpy p = arcpy.mp.ArcGISProject("CURRENT") m = p.activeMap parent_group_name = "YourParentGroupName" def get_immediate_children(parent_lyr): parent_path = parent_lyr.longName + "\\" return [l for l in m.listLayers() if l.longName.startswith(parent_path) and l.longName.replace(parent_path, "").count("\\") == 0] def sort_top_level_only(group_lyr): children = get_immediate_children(group_lyr) if not children: return # Sort the list of layers and group names A-Z sorted_children = sorted(children, key=lambda x: x.name.lower()) # 1. Anchor the first item (A) at the top of the parent group actual_first_child = get_immediate_children(group_lyr)[0] if sorted_children[0] != actual_first_child: m.moveLayer(actual_first_child, sorted_children[0], "BEFORE") # 2. Move every other item (including groups) AFTER the one before it ref_layer = sorted_children[0] for i in range(1, len(sorted_children)): target_layer = sorted_children[i] m.moveLayer(ref_layer, target_layer, "AFTER") ref_layer = target_layer # Recursive call removed: nested groups won't be internally sorted # Execution target = next((l for l in m.listLayers() if l.isGroupLayer and l.name == parent_group_name), None) if target: sort_top_level_only(target) print("Top-level alphabetical sort complete.")
To sort alphabetically on only one group, I think your only option is python. You can use this script. Open the python window from the analysis tab, paste this code, and enter your group name. If this is something you do often you could make this a quick script tool and just have the group name the only parameter.
import arcpy m = arcpy.mp.ArcGISProject("CURRENT").activeMap # Replace "Your Group Name" with your group layer's exact name group = m.listLayers("Your Group Name")[0] # Sort direct children of the group alphabetically sorted_layers = sorted(group.listLayers(), key=lambda l: l.name) for i, lyr in enumerate(sorted_layers): if i == 0: m.moveLayer(group.listLayers()[0], lyr, "BEFORE") else: m.moveLayer(sorted_layers[i - 1], lyr, "AFTER")
Thanks a lot, it did sort the folder alphabetically, but it didn't distinguish between a group or a single layer in the folder, i work with a lot of city planning layers, and they come in a packeges of polygons, lines, points and raster, and the scripts jambles it up,
Maybe i can arrange the data differntly.
Thanks anyway
Are you saying you have one parent group that you want to sort, which contains both individual layers and nested groups? And within each nested groups, the items should be sorted, but also those group names themselves should be sorted along with the single items in the parent? If so, try this
import arcpy p = arcpy.mp.ArcGISProject("CURRENT") m = p.activeMap parent_group_name = "YourParentGroupName" def get_immediate_children(parent_lyr): parent_path = parent_lyr.longName + "\\" return [l for l in m.listLayers() if l.longName.startswith(parent_path) and l.longName.replace(parent_path, "").count("\\") == 0] def sort_hierarchy(group_lyr): children = get_immediate_children(group_lyr) if not children: return # Sort A-Z sorted_children = sorted(children, key=lambda x: x.name.lower()) # We use the first sorted item as the "anchor" # We move it to the top of the group by moving it BEFORE the current first child actual_first_child = get_immediate_children(group_lyr)[0] if sorted_children[0] != actual_first_child: m.moveLayer(actual_first_child, sorted_children[0], "BEFORE") # Now move every other item AFTER the one before it ref_layer = sorted_children[0] for i in range(1, len(sorted_children)): target_layer = sorted_children[i] m.moveLayer(ref_layer, target_layer, "AFTER") ref_layer = target_layer # Recurse into nested groups if target_layer.isGroupLayer: sort_hierarchy(target_layer) # Final check for the anchor item if it's a group if sorted_children[0].isGroupLayer: sort_hierarchy(sorted_children[0]) # Execution target = next((l for l in m.listLayers() if l.isGroupLayer and l.name == parent_group_name), None) if target: sort_hierarchy(target) print("Alphabetical sort complete.")
Hi Brennan, first thing I would like to say a big thank you for your replys, you don't have to help me and it's very appreciated.
We are getting closer with your precise qustions, I have a parent group with individual layers and if i understand correctly nested layers, what i'm looking to do is organize the individual and the nested layers alphabetically, without sorting the order inside the nested groups and keeping them inside each group (or each nest if you can use the term this way).
I'v added a simple example from the layer "plans", inside i made groups for individual projects, that contain differnt types of layers (i.e raster/polygons/lines and so on).
Thanks again for all the help
Works like a charm.
Great, thanks a lot.
New idea: Sort layers in group layer alphabetically
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.