Search code examples
adobe-illustratoralphabetical-sort

A Script To Sort Objects Alphabetically Within A Layer In Adobe Illustrator CC?


I'm not literate about programming but I need a script to help me alphabetically sort objects in a layer in Adobe Illustrator. My vector is a map having 1 layer with all the objects I need to sort at the same level. Here is an image. These objects will be essentially paths, compound paths and groups. The ideal outcome is to sort the selected objects alphabetically. But any script that does the job is welcome.

In my investigation, I just found the same question from the Adobe Community but the scripts available in this page (https://community.adobe.com/t5/illustrator-discussions/how-to-sort-objects-alphabetically-inside-of-a-layer/m-p/6521187) end up with some errors like "No such... ". So, I hope this will help my helper as I don't know how to fix the problem from these scripts.

[The image illustrates how my vector is organized]


Solution

  • As far as I can see from the screenshot the 'West Sussex' is a group.

    You can try to select the group and run this script:

    var group = app.selection[0];
    var len = group.pageItems.length;
    
    for (var j=0; j<len; j++) {
        var items = [];
        for (var i=0; i<group.pageItems.length; i++) items.push(group.pageItems[i]);
    
        for (var i=0; i<items.length-1; i++) {
            if (items[i].name > items[i+1].name) {
                items[i].move(items[i+1], ElementPlacement.PLACEAFTER);
                i++;
            }
        }
    }
    

    The code is not exactly a best practice but it looks like it does the job.