[Tex/LaTex] Index sorting where numbers are indexed

indexingsorting

I have a vast document where I have a separate index of dates (really only the year or year range of particular events).
I'm using memoir.

Sometimes the year is known exactly

\index[dates]{1875!Event xyz}

Sometimes the year is a range

\index[dates]{1822--1867!Event abc}

These sort in index as

1822-1897
Event xxxx 77,87
Event pppp 73

1829-1844
Event zzz 79

1756
Event xxx2 99

In other words, date ranges sort alphabetically and are separated from the single numeric entries which sort numerically yielding an odd ordering. Is there any way I get numbers to sort alphabetically and not numerically so that I can get ordering

1
1–5
2
3
3–5
4

rather than

1–5
3–5
1
2
3
4

The index entries are via a macro

\newcommand{\indexdate}[2]{#1\index[dates]{#1!{#2}}}

And so a single change to the macro might achieve what I want without having to manually edit every one of a couple of thousands of entries to impose a sort order.

I have hit a roadblock on this one. I don't think a MWE would be of additional use here, but happy to post one.

Best Answer

Makeindex sorts numbers in numerical order:

1
9
10

However ranges are not numbers. The range symbol -- prevents Makeindex to take such an entry as number. These entries are sorted in lexical order. Also they precede the numbers.

You can specify a sort key for Makeindex. If the sort key is a number, then the number of digits should be the same for years and year ranges, e.g.:

\newcommand{\indexdate}[2]{#1\index[dates]{#10000@#1!#2}}
\newcommand{\indexdaterange}[3]{#1--#2\index[dates]{#1#2@#1--#2!#3}}

The sortkey is the entry before @. The sort key of a year range is just the two four-digit years. In case of a year, the sort key is the four-digit year, followed by four zeros to fill the eight digits for all numerical sort keys.

Alternatively the sort key can add a non-digit, then the entries are sorted in lexical order, with four-digit years, the numerical order is not really needed:

\newcommand{\indexdate}[2]{#1\index[dates]{#1x@#1!#2}}
Related Question