Copy link to clipboard
Copied
Fixed
var isLowerFirst = false; // Set it to false if you want to place the layers with the first uppercase letter above
var isReverse = false; // Set it to false if you want to sort names in reverse order
// Convert ILST collection into standard Array so we can use Array methods
function get(type, parent) {
if (arguments.length == 1 || !parent) parent = app.activeDocument;
var result = [];
if (!parent[type]) return result;
for (var i = 0; i < parent[type].length; i++) result.push(paren...
Copy link to clipboard
Copied
Hi,
By looking at your image, it does not seems like these are sublayers. I am not sure on what conditions do you want to sort. A question for you : Do items inside the Layer 1 has name with numbers?
If you are looking to sort layers, may be following link will be helpful. Following is using alphabets to sort.
https://gist.github.com/KennyRedman/b8582079dabc1be17b05
Copy link to clipboard
Copied
oh yes, i guess sublayers are different. these are numbers / fonts i have on my designs. i guess i can put them into differnt layers and sort the layers . Thanks a lot for your time and have a good day ahead
Copy link to clipboard
Copied
More link that could be helpful
https://community.adobe.com/t5/illustrator-discussions/sort-layers-alphabetically/td-p/12350421
Copy link to clipboard
Copied
Many thanks
Copy link to clipboard
Copied
I wanted to clarify. "Sublayers" - do you mean objects inside layer? Because objects and sublayers are different terms. Do you want to sort objects by name inside active layer?
Copy link to clipboard
Copied
yes , sorry my wrong lingo - items inside a layer
Copy link to clipboard
Copied
I used Tom Scharstein's (@Inventsable) layer sorting code and made objects in the active layer sort with textFrame content and custom names. The default names (not really real names) <rectangle>, <group> are not sorted
var isLowerFirst = false; // Set it to false if you want to place the layers with the first uppercase letter above
var isReverse = false; // Set it to false if you want to sort names in reverse order
// Convert ILST collection into standard Array so we can use Array methods
function get(type, parent) {
if (arguments.length == 1 || !parent) parent = app.activeDocument;
var result = [];
if (!parent[type]) return result;
for (var i = 0; i < parent[type].length; i++) result.push(parent[type][i]);
return result;
}
// Polyfill for Array.forEach
Array.prototype.forEach = function (callback) {
for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};
// Check string
function isEmpty(s) {
return s.replace(/\s/g, '').length == 0;
}
// Get pageItem name
function getName(e) {
if (e.typename === "TextFrame" && isEmpty(e.name) && !isEmpty(e.contents)) {
return e.contents;
} else if (e.typename === "SymbolItem" && isEmpty(e.name)) {
return e.symbol.name;
} else {
return e.name;
}
}
function sortLayerItems(parent, isReverse) {
if (arguments.length == 1 || !isReverse) isReverse = false;
function isUpperCase(text) {
return /[A-Z]/.test(text.charAt(0))
}
function isLowerCase(text) {
return /[a-z]/.test(text.charAt(0))
}
function hasMixedCase(a, b) {
if (a.charAt(0).toLowerCase() !== b.charAt(0).toLowerCase()) return false;
return isUpperCase(a) && isLowerCase(b) ? true : isLowerCase(a) && isUpperCase(b);
}
function handleMixedCase(a, b) {
var result = a === b ? 0 : isUpperCase(a) ? 1 : -1;
return isLowerFirst ? result : result * -1;
}
get("pageItems", parent)
.sort(function (a, b) {
var aName = getName(a);
var bName = getName(b);
return hasMixedCase(aName, bName) ?
handleMixedCase(aName, bName) :
aName.toLowerCase().localeCompare(bName.toLowerCase())
})
.forEach(function (e) {
e.zOrder(isReverse ? ZOrderMethod.BRINGTOFRONT : ZOrderMethod.SENDTOBACK);
});
}
sortLayerItems(activeDocument.activeLayer, isReverse);
Copy link to clipboard
Copied
This is amazing Sergey - works perfectly. A huge thank you !!!! HAve a great day ahead. You are my hero today.
Copy link to clipboard
Copied
Seems to make many in numerical order then start more batches in numerical order . it still helps me in some ways as all the numbers arent so jumbled but for some reason doesnt put them all in order. After 22 it starts again
  
Copy link to clipboard
Copied
Fixed
var isLowerFirst = false; // Set it to false if you want to place the layers with the first uppercase letter above
var isReverse = false; // Set it to false if you want to sort names in reverse order
// Convert ILST collection into standard Array so we can use Array methods
function get(type, parent) {
if (arguments.length == 1 || !parent) parent = app.activeDocument;
var result = [];
if (!parent[type]) return result;
for (var i = 0; i < parent[type].length; i++) result.push(parent[type][i]);
return result;
}
// Polyfill for Array.forEach
Array.prototype.forEach = function (callback) {
for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};
// Check string
function isEmpty(s) {
return s.replace(/\s/g, '').length == 0;
}
// Get pageItem name
function getName(e) {
if (e.typename === "TextFrame" && isEmpty(e.name) && !isEmpty(e.contents)) {
return e.contents;
} else if (e.typename === "SymbolItem" && isEmpty(e.name)) {
return e.symbol.name;
} else {
return e.name;
}
}
function sortLayerItems(parent, isReverse) {
if (arguments.length == 1 || !isReverse) isReverse = false;
function isUpperCase(text) {
return /[A-Z]/.test(text.charAt(0))
}
function isLowerCase(text) {
return /[a-z]/.test(text.charAt(0))
}
function hasMixedCase(a, b) {
if (a.charAt(0).toLowerCase() !== b.charAt(0).toLowerCase()) return false;
return isUpperCase(a) && isLowerCase(b) ? true : isLowerCase(a) && isUpperCase(b);
}
function handleMixedCase(a, b) {
var result = a === b ? 0 : isUpperCase(a) ? 1 : -1;
return isLowerFirst ? result : result * -1;
}
get("pageItems", parent)
.sort(function (a, b) {
var aName = getName(a);
var bName = getName(b);
if (!isNaN(aName) && !isNaN(bName)) {
return 1 * aName - 1 * bName;
} else {
return hasMixedCase(aName, bName) ?
handleMixedCase(aName, bName) :
aName.toLowerCase().localeCompare(bName.toLowerCase())
}
})
.forEach(function (e) {
e.zOrder(isReverse ? ZOrderMethod.BRINGTOFRONT : ZOrderMethod.SENDTOBACK);
});
}
sortLayerItems(activeDocument.activeLayer, isReverse);
Copy link to clipboard
Copied
Perfectomondo Thank you very very much for taking time out to do this. It encourages me to help others when i can. Have a lovely night .
Copy link to clipboard
Copied
New version with UI available here https://github.com/creold/illustrator-scripts/blob/master/jsx/SortLayerItems.jsx
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more