• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to sort layers by 123, instead abc

Explorer ,
Aug 01, 2016 Aug 01, 2016

Copy link to clipboard

Copied

I have script. wich sort layers by abc order.

#target "Illustrator"

/**

* sort layers (with sublayer)

*/

var doc = app.documents[0];

var lay = doc.layers;

var array_primary = [];

for (var i=0, iL=lay.length; i < iL ; i++) {

  if (lay.layers.length === 0) {

  array_primary.push( lay.name );

  }

  else {

  array_primary.push( lay.name );

  var array_secondary = [];

  for (var j=0, jL=lay.layers.length; j < jL ; j++) {

  array_secondary.push(lay.layers.name);

  };

  var result2 = array_secondary.sort( function (a,b) { return a > b });

  // sort sublayers

  sort_layer (lay, result2);

  }

};

var result1 = array_primary.sort( function(a,b) { return a > b } );

// sort layers

sort_layer (doc, result1);

function sort_layer (obj, array) {

  for (var ri=0, riL=array.length; ri < riL ; ri++) {

  obj.layers.getByName(array[ri]).zOrder( ZOrderMethod.SENDTOBACK );

  };

}

For example i have layer names:

Layer # 116

Layer # 12

Layer # 117

Layer # 32

Layer # 3

This script sorting them like:

Layer # 116

Layer # 117

Layer # 12

Layer # 3

Layer # 32

But i want to make it to increasing:

Layer # 3

Layer # 12

Layer # 32

Layer # 116

Layer # 117

Please, help me to make it right!

TOPICS
Scripting

Views

920

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Aug 01, 2016 Aug 01, 2016

Ch ch ch check it out!

The sorting function is changed to take out non-numeric characters and then parse int to be able to sort by number vs string.

#target illustrator-20

function test(){

  /**

  * sort layers (with sublayer)

  */

  function sortNumeric(a, b){

    a = a.replace(/[^\d]+/g, '');

    b = b.replace(/[^\d]+/g, '');

    return parseInt(a) > parseInt(b);

  };

  

  var doc = app.documents[0];

  var lay = doc.layers;

  var array_primary = [];

  for (var i=0, iL=lay.length; i < iL ; i++) {

    if (l

...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Aug 01, 2016 Aug 01, 2016

Copy link to clipboard

Copied

Ch ch ch check it out!

The sorting function is changed to take out non-numeric characters and then parse int to be able to sort by number vs string.

#target illustrator-20

function test(){

  /**

  * sort layers (with sublayer)

  */

  function sortNumeric(a, b){

    a = a.replace(/[^\d]+/g, '');

    b = b.replace(/[^\d]+/g, '');

    return parseInt(a) > parseInt(b);

  };

  

  var doc = app.documents[0];

  var lay = doc.layers;

  var array_primary = [];

  for (var i=0, iL=lay.length; i < iL ; i++) {

    if (lay.layers.length === 0) {

    array_primary.push( lay.name );

    }

    else {

    array_primary.push( lay.name );

    var array_secondary = [];

    for (var j=0, jL=lay.layers.length; j < jL ; j++) {

    array_secondary.push(lay.layers.name);

    };

    var result2 = array_secondary.sort( sortNumeric);

    // sort sublayers

    sort_layer (lay, result2);

    }

  };

  var result1 = array_primary.sort( sortNumeric );

  

  

  // sort layers

  sort_layer (doc, result1);

  

  

  function sort_layer (obj, array) {

    for (var ri=0, riL=array.length; ri < riL ; ri++) {

    obj.layers.getByName(array[ri]).zOrder( ZOrderMethod.SENDTOBACK );

    };

  }

};

test();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 01, 2016 Aug 01, 2016

Copy link to clipboard

Copied

LATEST

Dear Silly-V!

U giving me a hand again! I glad to know u, as good man and amazing script ninja!

Thank u very much, it works!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines