# Reordering the index

When I index a molecular formula, I use the syntax \index{C8H12O4} and \index{C10H16O5}. The resulting output will have C10 before the C8 compound. What I understand is: in the second entry, C is followed by 1, whereas in the first case, C is followed by 8. 1 is less than 8, hence C10 comes before C8.

The question is how to reorder so that C with lower number comes first and then the other. In the above case, C8 first, followed by C10.

A related question is: the CAS numbers “100-51-7” will appear before “88-45-6”. How to make it work the other way round?

Here is my MWE

\documentclass[a4paper,10pt]{book}
\usepackage{imakeidx}  % as i have multiple indexing
\makeindex  % as i have multiple indexing
\begin{document}

The illustrative example to show the problem I'm facing with indexing. C8H12O4\index{C8H12O4} and C10H16O5\index{C10H16O5} are interesting compounds. The resulting output will have C10 above the C8 compound.

Also checked for CAS numbers: 100-51-7\index{100-51-7} and 88-45-6\index{88-45-6} has to be indexed in the reverse order.  First 88-45-6, then 100-51-7.

\printindex
\end{document}

-
It is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This will also serve as a test case and ensure that the solution actually works for you. That way those trying to help can focus on solution as opposed to setting up the problem test case. –  Peter Grill Mar 13 '12 at 3:45
@PeterGrill, i have included the MWE. –  Anil Mar 13 '12 at 4:33

First, you need to do a small amount of planning to come up with the right scheme. Then, the \index command allows a parameter in the form {sortable@printable}, so you just add a sufficient number of leading zeros to the index sort key, like this:

C8H12O4\index{C08H12O4@C8H12O4} and C10H16O5\index{C10H16O5}
100-51-7\index{100-51-7} and 88-45-6\index{088-45-68@8-45-6}


Take, for example, the case of 88-45-46. If we can assume that the largest number will be 999-999-99, then we code it twice, once for printing, and once for sorting. The print key is "as-is": 88-45-6, but the sort key is padded with appropriate leading zeros: 088-045-06. Now, to index and sort it into the right place, all we need is:

\index{088-045-06@88-45-6}


(I hope that clarifies it a bit better)

-
@ Brent.Longborough, How to do this {sortable@printable}? Can you elaborate more. –  Anil Mar 13 '12 at 9:49
@Anil: I added a better-explanation (I hope!). –  Brent.Longborough Mar 13 '12 at 10:35
I tried this suggestion and works well. Thank you. –  Anil Mar 14 '12 at 4:55
@Anil: Thank you for your courtesy. I had already interpreted the acceptance of my answer as adequate thanks (which is what most of us tend to do here), so please feel at ease not to write specific thankyou comments. –  Brent.Longborough Mar 14 '12 at 8:06
@Anil: Sorry, now I re-read that, it comes across as a bit rude and grumpy - not my intention. What I meant was this: it's always lovely to see positive comments, but the community doesn't require it. So, since upvoting and accepting answers already includes an implied thankyou, not making an explicit thankyou comment is not considered a lack of good manners. –  Brent.Longborough Mar 14 '12 at 19:17