0
Explorer
,
/t5/illustrator-discussions/can-someone-share-script-that-reorders-objects-in-a-layer-alphabetically-from-z-a/td-p/12881245
Apr 14, 2022
Apr 14, 2022
Copy link to clipboard
Copied
I do have some figures with names that needs to be ordered alphabetically in the layer (random name) from Z-A (sometimes can be 500+ objects), it can be torture to do it manualy.
Endgoal Example:
I select all the objects in the layer, run the scripts and every selected object (or all objects in the layer) gets alphabetically ordered from Z-A.
 
TOPICS
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
1 Correct answer
Guide
,
Apr 15, 2022
Apr 15, 2022
- I presume you're aware that when rearranging the layer panel, you're not just rearranging the names of the items, but rearranging the Z-order of the items.
- This is a bubble algorithm, so it may be slow if there are hundreds of items.
var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
if (!doc.layers[i].locked && doc.layers[i].visible) {
rearrange(doc.layers[i].pathItems);
}
}
function rearrange(items){
for (var i = items.length - 1; i > -1; i--){
...
Explore related tutorials & articles
Guide
,
/t5/illustrator-discussions/can-someone-share-script-that-reorders-objects-in-a-layer-alphabetically-from-z-a/m-p/12881424#M318446
Apr 15, 2022
Apr 15, 2022
Copy link to clipboard
Copied
- I presume you're aware that when rearranging the layer panel, you're not just rearranging the names of the items, but rearranging the Z-order of the items.
- This is a bubble algorithm, so it may be slow if there are hundreds of items.
var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
if (!doc.layers[i].locked && doc.layers[i].visible) {
rearrange(doc.layers[i].pathItems);
}
}
function rearrange(items){
for (var i = items.length - 1; i > -1; i--){
for (var j = 1; j <= i; j++){
if (alphanumCase(items[j - 1].name, items[j].name) < 0){
items[j].moveBefore(items[j - 1]);
}
}
}
}
// http://www.davekoelle.com/files/alphanum.js
function alphanumCase(a, b) {
function chunkify(t) {
var tz = new Array();
var x = 0, y = -1, n = 0, i, j;
while (i = (j = t.charAt(x++)).charCodeAt(0)) {
var m = (i == 46 || (i >=48 && i <= 57));
if (m !== n) {
tz[++y] = "";
n = m;
}
tz[y] += j;
}
return tz;
}
var aa = chunkify(a.toLowerCase());
var bb = chunkify(b.toLowerCase());
for (x = 0; aa[x] && bb[x]; x++) {
if (aa[x] !== bb[x]) {
var c = Number(aa[x]), d = Number(bb[x]);
if (c == aa[x] && d == bb[x]) {
return c - d;
} else return (aa[x] > bb[x]) ? 1 : -1;
}
}
return aa.length - bb.length;
}
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
D.S..
AUTHOR
Explorer
,
LATEST
/t5/illustrator-discussions/can-someone-share-script-that-reorders-objects-in-a-layer-alphabetically-from-z-a/m-p/12881532#M318454
Apr 15, 2022
Apr 15, 2022
Copy link to clipboard
Copied
Exactly what I needed.
I am aware of the rearange you mentioned.
Thank you! 🙂
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

