Skip to main content
christype
Known Participant
June 13, 2020
Answered

Organise object layer order based on object X-position

  • June 13, 2020
  • 1 reply
  • 2386 views

I'm looking for a way to select a large number of objects and automatically sort them in the layer palette so the left-most object is at the top of the layer order, the next is arranged under it and so on... (until the right-most is at the bottom).

 

I've seen some very old scripts around but none seem to work.

 

...Can't seem to attach a file. It's basically dozens of vertical lines (separate paths created with a blend, made into a compound path, then cut out out into a shape using pathfinder. This last part breaks the order of the objects)

 

This topic has been closed for replies.
Correct answer femkeblanco

Sorry.  I didn't test it for selected items.  You are right, it doesn't work for selected items (I'm not sure why off the top of my head).  However, it should work for all paths in the document:

function sortLtoR(items){
  for (var i = (items.length - 1); i >= 0; i--){
      for (var j = 1; j <= i; j++){
          if (items[j-1].left < items[j].left){
              items[j].moveBefore(items[j-1]);
          }
      }
  }
}
sortLtoR(app.activeDocument.pathItems);

 

1 reply

Charu Rajput
Community Expert
Community Expert
June 13, 2020

Hi,

Try following snippet

 

function test() {
    if (!app.documents.length) {
        alert("You must have a document open.");
        return;
    }
    var docRef = app.activeDocument;
    var sel = docRef.selection;
    if (!sel.length) {
        alert("You must make a selection.");
        return;
    }

    function sortHorizonally(items) {
        var topMost, curX, delIndex, curItem, tempItems = [];
        for (var x = 0, len = items.length; x < len; x++) {
            tempItems.push(items[x]);
        }

        while (tempItems.length) {
            topMost = tempItems[0];
            curX = topMost.left
            delIndex = 0;
            for (var x = 1, len = tempItems.length; x < len; x++) {
                curItem = tempItems[x];
                if (curItem.left > curX) {
                    topMost = curItem;
                    curX = curItem.left;
                    delIndex = x;
                }
            }
            topMost.zOrder(ZOrderMethod.BRINGTOFRONT);
            tempItems.splice(delIndex, 1);
        }
    }
    sortHorizonally(sel);
}

test();

 

 

Main Script source : https://community.adobe.com/t5/illustrator/auto-arrange-per-y-value-position/td-p/10024991?page=1

Credit: @williamadowling 

 

But script at above link is not working as there is some error, while moving thread from old forum to new forum.

 

Let us know if above snippet works for you.

Best regards
femkeblanco
Legend
June 13, 2020

Try this

 

// select items to sort
function sortLtoR(items){
    for (var i = (items.length - 1); i >= 0; i--){
        for (var j = 1; j <= i; j++){
            if (items[j-1].left < items[j].left){
                items[j].moveBefore(items[j-1]);
            }
        }
    }
}
sortLtoR(selection);