SortParagraphs (not working)
Hello;
I can't make a correct order. The methods I used ("sortparagraph.jsx and smartSort" ) don't work! thank you

Hello;
I can't make a correct order. The methods I used ("sortparagraph.jsx and smartSort" ) don't work! thank you

Hi @uniq1, here is a little script I wrote to handle your example. It works by padding out the numbers with zeros and sorting that. Select the text or text frame and run script. Let me know how it works for you.
- Mark
function main() {
sortParagraphsByPaddingNumbers(app.activeDocument.selection[0]);
/**
* Sort paragraphs by padding numbers.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/sortparagraphs-not-working/m-p/13045173
* @param {TextFrame|Text} items - any Indesign object with 'paragraphs' property
*/
function sortParagraphsByPaddingNumbers(items) {
if (!items.hasOwnProperty('paragraphs')) {
alert('Please select some text or a text frame and try again.')
return;
}
// add a carriage return if necessary
if (items.paragraphs.item(-1).characters.item(-1).contents != '\u000D')
items.paragraphs.item(-1).insertionPoints.item(-1).contents = '\u000D';
var paragraphs = items.paragraphs.everyItem().getElements(),
sorter = [],
afterPoint = items.paragraphs[paragraphs.length - 1].insertionPoints[-1];
// make a sortable version of each paragraph
for (var i = 0; i < paragraphs.length; i++) {
var parts = paragraphs[i].contents
// remove carriage returns, linefeeds
.replace(/[\u000D\u000A]/g, '')
// separate numbers
.split(/(\d+)/g);
// make a sortable string out of the parts
// by padding the numbers with zeros
for (var j = 0; j < parts.length; j++)
if (!isNaN(Number(parts[j])))
parts[j] = ('0000000000' + parts[j]).slice(-10);
// combine into a handy object
sorter[i] = {
paragraph: paragraphs[i],
sortable: parts.join('')
};
}
// sort using the sortable string
sorter.sort(function (a, b) {
if (a.sortable > b.sortable) return 1;
else if (a.sortable < b.sortable) return -1;
else return 0
});
// duplicate paragraphs in sorted order
for (var i = 0; i < sorter.length; i++)
resolve(sorter[i].paragraph.toSpecifier()).duplicate(LocationOptions.AFTER, afterPoint);
// remove the unsorted paragraphs
for (var i = paragraphs.length - 1; i >= 0; i--)
resolve(paragraphs[i].toSpecifier()).remove();
function isNaN(n) {
return n !== n;
}
}
}
app.doScript(
main,
ScriptLanguage.JAVASCRIPT,
undefined,
UndoModes.ENTIRE_SCRIPT,
"Sort Paragraphs"
);
Just for interest: this shows example of the padded numbers paragraphs that script uses to sort:

Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.