Skip to main content
zawij
New Participant
September 23, 2014
Answered

How to sort objects alphabetically (inside of a layer)?

  • September 23, 2014
  • 4 replies
  • 26340 views

How to sort objects alphabetically (inside of a layer) on layer list?

I have already tried scripts for Layer sorting. They work indeed for the layers, but not the objects themselves packed inside.

This topic has been closed for replies.
Correct answer Jung K Kwon

 

// sort layers and sublayers by Jung K Kwon
// https://community.adobe.com/t5/illustrator-discussions/how-to-sort-objects-alphabetically-inside-of-a-layer/m-p/13462829#M349907

//#target "Illustrator"
var doc = app.activeDocument;
var Top_all_layers = doc.layers;
Arrange_Layers(doc, Top_all_layers);
redraw();

function Arrange_Layers(where, T_Layers_OBJ) {
    var temp_L_names = new Array();
    for (var i = 0; i < T_Layers_OBJ.length; i++) {
        temp_L_names.push(T_Layers_OBJ[i].name);
    }
    sort_layer(where, temp_L_names.sort(naturalCompare));;
    for (var i = 0; i < T_Layers_OBJ.length; i++) {
        if (T_Layers_OBJ[i].layers.length != 0) {
            Arrange_Layers(T_Layers_OBJ[i], T_Layers_OBJ[i].layers);
        }
    }
    return;
}

function sort_layer(where, Layers_Array) {
    var temp_P_names = Array();
    var Sorted_temp_P = Array();
    for (var k = 0; k < Layers_Array.length; k++) {
        var temp_layer = where.layers.getByName(Layers_Array[k]); //
        var status_Lock = temp_layer.locked;
        var status_Preview = temp_layer.preview;
        var status_visible = temp_layer.visible;
        temp_layer.locked = false;
        temp_layer.preview = true;
        temp_layer.visible = true;
        temp_layer.zOrder(ZOrderMethod.SENDTOBACK);
        temp_layer.locked = status_Lock;
        temp_layer.preview = status_Preview;
        temp_layer.visible = status_visible;
    }
    for (var k = 0; k < Layers_Array.length; k++) {
        var temp_layer = where.layers.getByName(Layers_Array[k]); //
        var temp_items = temp_layer.pageItems;
        for (var l = 0; l < temp_items.length; l++) {
            temp_P_names.push(temp_items[l].name);
        }
        Sorted_temp_P = temp_P_names.sort(naturalCompare);
        try {
            for (var l = 0; l < Sorted_temp_P.length; l++) {
                //$.writeln(Sorted_temp_P[l], "--", temp_items);
                var temp_OBJ = temp_layer.pageItems.getByName(Sorted_temp_P[l]);
                temp_OBJ.zOrder(ZOrderMethod.SENDTOBACK);
                //
            } //
        } catch (e) {
            //alert(e); 
        }
    }
    //
}
//
//
function naturalCompare(a, b) {
    var ax = [],
        bx = [];
    a.replace(/(\d+)|(\D+)/g, function(_, $1, $2) {
        ax.push([$1 || Infinity, $2 || ""])
    });
    b.replace(/(\d+)|(\D+)/g, function(_, $1, $2) {
        bx.push([$1 || Infinity, $2 || ""])
    });
    while (ax.length && bx.length) {
        var an = ax.shift();
        var bn = bx.shift();
        var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
        if (nn) return nn;
    }
    return ax.length - bx.length;
}

 

4 replies

New Participant
January 10, 2024

Dangit; not working for me. Have there been any changes? The only code I see to copy is the 2015 post, and it's just radio silence after I run it; no errors, but no change to the layers. 

 

 

CarlosCanto
Adobe Expert
January 10, 2024

the script from Sep 23, 2015 is the one that works

Known Participant
April 29, 2022

I keep getting this error. Any help?

New Participant
August 18, 2022

Did you ever get past this? I'm getting the same issue.

 

New Participant
January 3, 2023

Same issue still

Jung K Kwon
Jung K KwonCorrect answer
Participating Frequently
September 24, 2015

 

// sort layers and sublayers by Jung K Kwon
// https://community.adobe.com/t5/illustrator-discussions/how-to-sort-objects-alphabetically-inside-of-a-layer/m-p/13462829#M349907

//#target "Illustrator"
var doc = app.activeDocument;
var Top_all_layers = doc.layers;
Arrange_Layers(doc, Top_all_layers);
redraw();

function Arrange_Layers(where, T_Layers_OBJ) {
    var temp_L_names = new Array();
    for (var i = 0; i < T_Layers_OBJ.length; i++) {
        temp_L_names.push(T_Layers_OBJ[i].name);
    }
    sort_layer(where, temp_L_names.sort(naturalCompare));;
    for (var i = 0; i < T_Layers_OBJ.length; i++) {
        if (T_Layers_OBJ[i].layers.length != 0) {
            Arrange_Layers(T_Layers_OBJ[i], T_Layers_OBJ[i].layers);
        }
    }
    return;
}

function sort_layer(where, Layers_Array) {
    var temp_P_names = Array();
    var Sorted_temp_P = Array();
    for (var k = 0; k < Layers_Array.length; k++) {
        var temp_layer = where.layers.getByName(Layers_Array[k]); //
        var status_Lock = temp_layer.locked;
        var status_Preview = temp_layer.preview;
        var status_visible = temp_layer.visible;
        temp_layer.locked = false;
        temp_layer.preview = true;
        temp_layer.visible = true;
        temp_layer.zOrder(ZOrderMethod.SENDTOBACK);
        temp_layer.locked = status_Lock;
        temp_layer.preview = status_Preview;
        temp_layer.visible = status_visible;
    }
    for (var k = 0; k < Layers_Array.length; k++) {
        var temp_layer = where.layers.getByName(Layers_Array[k]); //
        var temp_items = temp_layer.pageItems;
        for (var l = 0; l < temp_items.length; l++) {
            temp_P_names.push(temp_items[l].name);
        }
        Sorted_temp_P = temp_P_names.sort(naturalCompare);
        try {
            for (var l = 0; l < Sorted_temp_P.length; l++) {
                //$.writeln(Sorted_temp_P[l], "--", temp_items);
                var temp_OBJ = temp_layer.pageItems.getByName(Sorted_temp_P[l]);
                temp_OBJ.zOrder(ZOrderMethod.SENDTOBACK);
                //
            } //
        } catch (e) {
            //alert(e); 
        }
    }
    //
}
//
//
function naturalCompare(a, b) {
    var ax = [],
        bx = [];
    a.replace(/(\d+)|(\D+)/g, function(_, $1, $2) {
        ax.push([$1 || Infinity, $2 || ""])
    });
    b.replace(/(\d+)|(\D+)/g, function(_, $1, $2) {
        bx.push([$1 || Infinity, $2 || ""])
    });
    while (ax.length && bx.length) {
        var an = ax.shift();
        var bn = bx.shift();
        var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
        if (nn) return nn;
    }
    return ax.length - bx.length;
}

 

Aristarco Palacios
Known Participant
June 12, 2017

Works great for me!

Script debugging window keeps appearing though. Is it because of the catch(e)? Or did I name it wrong and it is a .jsx and not a .js?

Thanks again!!

EDIT: Never mind, I found it!

CarlosCanto
Adobe Expert
September 23, 2014

post your script, someone will help you tweak it to work with the objects instead of layers,

captainfodder
Participating Frequently
January 13, 2015

Likewise, I'm interested to see if this can be resolved. The script I've been attempting to use is this: Click Here

Basically what I'm trying to achieve is a layer group by state, then by provider, then by city, then the dot representing the city.

Example:

  • Wyoming
    • Charter
      • Cable
        • Gillette, WY
        • Albin, WY
        • Additional Cities +

That above script so far will sort everything except the final (deepest) layer, the one with the object (my dot).

Disposition_Dev
Adobe Expert
January 13, 2015

do you know how many "layers" in the hierarchy there will be? or is that an unknown value? for example.. the "Town, WY" always be under "cable" which will always be under "charter" etc? or is it possible that there could be more or less levels within the hierarchy?