Suppose I would like to sort an index file called "xchp.idx" that includes the following information:
\indexentry{{Chp 1 } cherry}{1}
\indexentry{{Chp 1 } apple}{1}
\indexentry{{Chp 1 } banana}{1}
\indexentry{{Chp 2 } banana}{1}
\indexentry{{Chp 2 } cherry}{1}
\indexentry{{Chp 2 } apple}{1}
\indexentry{{Chp 10 } banana}{1}
\indexentry{{Chp 10 } cherry}{1}
\indexentry{{Chp 10 } apple}{1}
\indexentry{{Chp 12 } apple}{1}
\indexentry{{Chp 12 } cherry}{1}
\indexentry{{Chp 12 } banana}{1}
If I attempt such a sort operation using
makeindex xchp.idx
then I get
\item {Chp 1 } apple, 1
\item {Chp 1 } banana, 1
\item {Chp 1 } cherry, 1
\item {Chp 10 } apple, 1
\item {Chp 10 } banana, 1
\item {Chp 10 } cherry, 1
\item {Chp 12 } apple, 1
\item {Chp 12 } banana, 1
\item {Chp 12 } cherry, 1
\item {Chp 2 } apple, 1
\item {Chp 2 } banana, 1
\item {Chp 2 } cherry, 1
This is sorting everything lexicographically, whereas I would like the second token in each string to be sorted numerically like this:
\item {Chp 1 } apple, 1
\item {Chp 1 } banana, 1
\item {Chp 1 } cherry, 1
\item {Chp 2 } apple, 1
\item {Chp 2 } banana, 1
\item {Chp 2 } cherry, 1
\item {Chp 10 } apple, 1
\item {Chp 10 } banana, 1
\item {Chp 10 } cherry, 1
\item {Chp 12 } apple, 1
\item {Chp 12 } banana, 1
\item {Chp 12 } cherry, 1
To that end, I might try to use the "page_precedence" option in a style file called "xchp.ist" containing this:
page_precedence "anaan"
where a=alphabetic lower and n=numeric.
Here I might be tempted to complain, that while this option is mentioned here and there on stackexchange, there seems to be no clear documentation to its use...not even in Table 11.1 of "The LaTeX Companion" second edition. But then I would stop myself from complaining, and profusely apologize, because I am getting makeindex for free ... and so who am I to complain?
Now if I try
makeindex -s xchp.ist xchp.idx
I get
\item {Chp 1 } apple, 1
\item {Chp 1 } banana, 1
\item {Chp 1 } cherry, 1
\item {Chp 10 } apple, 1
\item {Chp 10 } banana, 1
\item {Chp 10 } cherry, 1
\item {Chp 12 } apple, 1
\item {Chp 12 } banana, 1
\item {Chp 12 } cherry, 1
\item {Chp 2 } apple, 1
\item {Chp 2 } banana, 1
\item {Chp 2 } cherry, 1
Which is still not what I want. Can anyone help me out? I suspect that xindy may have a solution. But that may be another post. For now, is there a way to perform the desired operation with makeindex?

makeindexwill perform a numerical sort, so you'd need the entries to be in the form\indexentry{1@{Chp 1 } cherry}{1}. – Nicola Talbot Nov 23 '17 at 22:44makeindexwithout changing the index file. Are you willing to use a pipe (sedetc)? Thepage_precedencesetting relates to the page number ordering (in the second argument of\indexentry). So if you have\indexentry{foo}{IX},\indexentry{foo}{10}and\indexentry{foo}{xii}thenpage_precedence "rnR"will put the lower-case roman numerals at the start of the page list, followed by the arabic numerals, followed by upper case roman numerals. So you end up withfoo, xii, 10, IX. – Nicola Talbot Nov 23 '17 at 23:13xindy? Interestingly, it seems to work out of the box. Trytexindy xchap.idx– David Purton Nov 24 '17 at 04:31xindy. – David Purton Nov 24 '17 at 04:56texindyrunning from withinlatexand have to run it manually. So it's meant to work fine. Asking a separate question is probably your best option. – David Purton Nov 24 '17 at 13:51makeindexis on the restricted list (that is, it's a trusted application) so it can be run using TeX's shell escape in restricted mode.xindyisn't on the restricted list (there's a comment about it intexmf.cnf) so it can't be run from the shell escape in restricted mode. It needs the unrestricted-shell-escapewhich is less secure. – Nicola Talbot Nov 24 '17 at 14:04texindy xchp.idxagain, and this time it worked giving me anxchp.indfile (not the error message I got before). And, as @DavidPurton said, it sorted correctly ("correctly" here meaning the way I egocentrically wanted it). I don't know what happened before. But the good news is, based on past experience, once an intermittent problem appears once, it will likely appear again. Something to look forward to. Many thanks to Nicola and David again, and to stackexchange, even though apparently it will not support me using @ tags in the same comment twice. – Daniel J. Greenhoe Nov 24 '17 at 22:55texindy xchp.idxon a much larger xelatex project with\indexentrystatements involving font size switching, typeface switching, Asian glyphs, a teckit mapping (for PinYin tone diacritic marks), and hyper-link support, like this ...Everything appears to work "correctly" right out of the box. Thank you again.
– Daniel J. Greenhoe Nov 24 '17 at 23:59