• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Need help to sort index pages using InDesign scrip...

# Need help to sort index pages using InDesign script

Community Beginner ,
Jun 23, 2024 Jun 23, 2024

Copied

Hi All,

I am looking for the solution to fix Index pages.

I have an array and need pages sorted as per below example:

var myArray = [5, 6, 7, 43, 45, 65, 66, 77, 98, 99, 100, 123, 155, 166]

output = [5ff, 43, 45, 65f, 77, 98ff, 123, 155, 166]

so logic is if there is page range continue more than 2 thfn "ff" will add and if its just more than one time then "f". I am unable to logic this loop so any help will be appreciated.

Thanks,

Shonky

TOPICS
Scripting

Views

141

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 Expert , Jun 23, 2024 Jun 23, 2024

Something like the following should work

``````function convertArray(arr) {
var i = 0;
while (i < arr.length) {
var start = i;
while (i + 1 < arr.length && arr[i] + 1 === arr[i + 1]) {
i++;
}
var end = i;
if (end - start > 1) {
arr[start] = arr[start] + 'ff';
// Remove all elements in the middle of the sequence
arr.splice(start + 1, end - start);
i = start + 1; // Move to the next element after ``````
...

5 Replies 5
Community Expert ,
Jun 23, 2024 Jun 23, 2024

Copied

As your array is already sorted - you just need to check if next element is +1 of the current, if it is, then store index of this element, then keep checking - if only +1 - then add "f" to the saved index - if there is more - then add "ff".

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 Beginner ,
Jun 24, 2024 Jun 24, 2024

Copied

Thanks Robert!

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 Expert ,
Jun 23, 2024 Jun 23, 2024

Copied

Something like the following should work

``````function convertArray(arr) {
var i = 0;
while (i < arr.length) {
var start = i;
while (i + 1 < arr.length && arr[i] + 1 === arr[i + 1]) {
i++;
}
var end = i;
if (end - start > 1) {
arr[start] = arr[start] + 'ff';
// Remove all elements in the middle of the sequence
arr.splice(start + 1, end - start);
i = start + 1; // Move to the next element after the modified one
} else if (end - start === 1) {
arr[start] = arr[start] + 'f';
// Remove the second element in the sequence
arr.splice(start + 1, 1);
i = start + 1; // Move to the next element after the modified one
} else {
i++;
}
}
return arr;
}

var arr = [5, 6, 7, 43, 45, 65, 66, 77, 98, 99, 100, 123, 155, 166];
convertArray(arr);
\$.writeln(arr);``````

-Manan

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 Beginner ,
Jun 24, 2024 Jun 24, 2024

Copied

Thanks Manan!!

Report

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

Copied

LATEST

Hi all,

For those who use IdExtenso, the module \$\$.PageRange supports the `f`, `ff` suffixes (reduxPair and reduxMore parameters). Full code here with all options explained:

https://github.com/indiscripts/IdExtenso/blob/master/etc/\$\$.PageRange.jsxlib#L519

(Note: this algorithm is used in IndexMatic³.)

Best,

Marc