is now part of CreativePro.com!

Sort an Index by Page Number

13

What if you use the index feature but need the result in page number order instead of alphabetical order? The easiest way to make this kind of list is probably to use the Table of Contents feature instead of the index feature. (Inside the Table of Contents dialog box, there is a checkbox labeled Sort Entries in Alphabetical Order… but you only see it when you click the More Options button inside that dialog box.)

However, if you already have your index, and you need to put it in chronological order (by page number) instead of alphabetical, you could use this three step process. Here’s my starting point:

Indexgrep1

First, I’m going to use the Find/Change dialog box to search for ^(.+?)\t(\d+?)$ and replace it with $2\t$1 which means “find all the text from the beginning of the paragraph up to a tab, and then followed by some numbers until the end of the paragraph, and then replace it with the second part (the numbers) then a tab, then the first part (the text up to the tab).”

Indexgrep2

Once you change all those, you can use the SortParagraphs.jsx script, which lives inside the Window > Utilities > Scripts menu (look inside the application folder, then the sample scripts and the javascripts folder):

Indexgrep3

All you need to do is select the text frame or text and then double-click that script in the panel. Poof! It’s in page number order.

Finally, step three is to reverse the order again, by again using a grep find/change. Here you can search for ^(\d+)\t(.+)$ and then replace it with $2~y$1

Indexgrep4

Note that in this case, I swapped the tab (\t) with a right-indent tab (~y) so that I don’t have to worry about a tab stop or where that page number is going to go (it’ll always go to the right margin). In the image above you’ll see that I have already performed the Change All.

Warning: There is one very important thing that you need to watch out for when doing these kinds of grep find/changes… if you have applied local formatting (font or color changes, for example) to the text, it will get all messed up! That’s because GREP appears to work on the underlying text separately from the formatting. Very frustrating. So: save your work before you do any kind of change all; and instead of applying local formatting, use Nested Styles and GREP Styles to format your text in these kinds of documents.

David Blatner is the co-founder of the Creative Publishing Network, InDesign Magazine, CreativePro Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at LinkedIn Learning (Lynda.com) are among the most watched InDesign training in the world.
You can find more about David at 63p.com

Follow on LinkedIn here
  • Daniel Ferens says:

    Thanks David!
    This was a great help.
    I really appreciate it.

  • Bart Van de Wiele says:

    Great work David!

  • Judy says:

    Sounds like a great option for working with index entries. Thanks, David! There is a company in the UK called Kerntiff Publishing that has produced a suite of add-ons with indexers in mind including one that allows professional indexers to create the index in more efficient, proprietary software such as SKY Professional or Cindex and import the completed index into InDesign, converting it to the correct tags as it goes. Check them out at https://www.kerntiff.co.uk/products-4-indesign/indexers-corner

  • Rob Peters says:

    Hi David,
    Nifty use of Grep-styles, this.
    Would you know of a way to let it work with something like:
    Item 46–49

    That would help me a lot.
    Thanks & best,
    Rob (The Netherlands)

    • Rob: I think you would need to expand the GREP expression to allow a number OR a dash or an en dash, such as ^(.+?)\t([\d–-]+?)$
      (the square brackets mean “OR”… that is, any one of those characters)

      • Rob Peters says:

        Thanks David!
        I’m almost there, as this takes care of the first part of the operation. But how do I get from
        46–49 Annemarie Busschers
        50–51 Hermien Buytendijk
        to
        Annemarie Busschers[tab]46–49
        Herzien Buytendijk[tab]50–51

        I can’t figure this out. Remember: I am a half-wit and you are a sort of genius.
        Tanks & best,
        Rob

      • Rob: It’s just the same process as the article above, I think. You should be able to replace it with $2\t$1
        That means “type the second part, then a tab, then the first part” (where “part” = the stuff in the parentheses)

      • Oh, wait, sorry, I misunderstood your question. To reverse it (put the number after), you would search for ^([\d–-]+)\t(.+)$
        and then replace it with $2~y$1

        You’re not a half-wit! :-) But I do encourage you to learn a little more about grep so you can make these little edits yourself. You can learn more here: indesignsecrets.com/grep

  • Rob Peters says:

    Thanks again David!
    Now everything works fine. I can now make the index I want for a 300+ page book.
    I’m trying to learn more about greps (they already save me a lot of time), but sometimes I just can’t get them right.
    You’re still a genius, though.
    Rob

  • Johan de Bakker says:

    Hi David,

    Thanks for you explanation about sorting indexes by page number. I understand more and more about grep, but I can’t figure out what I am supposed to do in the following case:
    I have an index which contains entries like:

    item1, 10, 50, 100
    item2, 15, 55, 105
    ,,,

    And I want to sort them by page number, resulting in;
    10 item1
    15 item2
    50 item1
    etc.
    So my question is: how should I break up my multiple string of page numbers by means of grep-codes (in your example you had entries with only one page number, but I want to do the same as you did with more than one page number)

    (By the way, I have a comma followed by a space instead of a tab after the text of the entries, but I think I know how to handle that one)

    Thanks
    Johan

  • Pradeep Kumar says:

    Hi David,

    Is there any way to add index to a word in a specific page avoiding other pages by using grep or by any scripts.

    For Example
    If we have a word “Phone” in document in page 23,64 its should add index to that page only and leave other page as it is.

    Please Suggest.

  • Kay H says:

    If you want to view entries in page order so as to see the entries on each page (just as a reference, not as a final product), you can copy and paste the text into a word processor, create a two-column table out of the text, and sort the table by the second column. You may need to replace whatever separates the index entry and the page numbers to create the two columns. For example, I needed to replace two spaces with a tab.

  • >