Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

Sort an Index by Page Number

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.

Tags
Related Articles
Comments

4 Comments on “Sort an Index by Page Number

  1. 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 http://www.kerntiff.co.uk/products-4-indesign/indexers-corner

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>