Once for each collection.
Just as I was about to answer this yesterday, my internet ran out, fortunately there is a bit of a point of difference.
To do this once for all collections, would add the root scene collections to bpy.data.collections and then sort the immediate children of each.
To sort all collections in blend in one fell swoop.
import bpy collections = bpy.data.collections[:] collections.extend(scene.collection for scene in bpy.data.scenes) for col in collections: for c in sorted(col.children, key=lambda c: c.name): col.children.unlink(c) col.children.link(c) Similarly to sort just a specific collection root, context.collection in example below (minus test for it being None)
import bpy context = bpy.context def all_colls(c): def walk(col): yield col for c in col.children: yield from walk(c) return set(walk(c)) assert(context.collection) # poll cannot be None. for col in all_colls(context.collection): for c in sorted(col.children, key=lambda c: c.name.lower()): col.children.unlink(c) col.children.link(c) once again ensuring we only sort the immediate children of each collection once.