Skip to main content
Noodles2456
Participant
August 29, 2021
Answered

Sort Layers alphabetically

  • August 29, 2021
  • 3 replies
  • 7152 views

Hello!

 

I'm trying to organize my layers in Illustrator aphabetically, and I can't find a script for it.  I've found several potential scripts here but I couldn't get them to work, and since my coding knowledge is extremely basic I can't really make one on my own.  Is there a script I can use to sort layers?

Correct answer Sergey Osokin

I found a script to sort layers alphabetically https://gist.github.com/KennyRedman/b8582079dabc1be17b05

3 replies

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
August 31, 2021

I found a script to sort layers alphabetically https://gist.github.com/KennyRedman/b8582079dabc1be17b05

femkeblanco
Legend
September 7, 2021

KennyRedman's script will prodoce the same result as the snippet I posted.  It's based on a Unicode alphabetical comparison using the > operator.  As I said, you'll get "Zoo" before "aardvark". 

Inventsable
Legend
September 7, 2021

You can use String.prototype.localeCompare while sorting to get the correct results:

 

var yourLayerNameArray = ["tiny", "Zoo", "aardvark"];
yourLayerNameArray.sort(function (a, b) {
  return a.localeCompare(b);
});
console.log(yourLayerNameArray);
// Returns as ["aardvark", "tiny", "Zoo"]
femkeblanco
Legend
August 30, 2021

For the simplest case, based on Unicode order (9 before A, Z before a and 10 before 2):

 

function alphabetise(a) {
    for (var i = 1; i < a.length; i++) {
        for (var j = i; j > 0; j--) {
            if (a[j].name < a[j-1].name) {
                a[j].moveBefore(a[j-1]);
            }
        }
    }
}
alphabetise(app.activeDocument.layers);

 

CarlosCanto
Community Expert
Community Expert
August 30, 2021

please post a link to the potential scripts and let us know why they're not working for you, and/or post the error codes you're getting.