Skip to main content
Participating Frequently
June 23, 2024
Answered

Need help to sort index pages using InDesign script

  • June 23, 2024
  • 3 replies
  • 629 views

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

This topic has been closed for replies.
Correct answer Manan Joshi

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

3 replies

Marc Autret
Legend
June 25, 2024

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

Manan JoshiCommunity ExpertCorrect answer
Community Expert
June 24, 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 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

-Manan
Veera_IdSAuthor
Participating Frequently
June 24, 2024

Thanks Manan!!

Robert at ID-Tasker
Legend
June 24, 2024

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".

 

Veera_IdSAuthor
Participating Frequently
June 24, 2024

Thanks Robert!