# How to sort glossaries entries by alphabetical order then by order of appereance

I would like my glossaries entries to be sorted by alphabetical order, and, if some entries have identical sort keys, by the order they are called in the text. Any way to do this ?

Here is the MWE, with the two entries div and commut exhibiting identical sort keys.

\documentclass[]{article}

\usepackage[
nogroupskip,
%           xindy,
]{glossaries}
% I compile with xindy, but I do not think it is relevant here.
% xindy -L english -C utf8 -I xindy -M % -t %.glg -o %.gls %.glo

\newglossaryentry{div}
{ name={\ensuremath{\protect\vec{\nabla}.}},
description={Divergence operator},
sort={0}
}

\newglossaryentry{commut}
{ name={\ensuremath{\protect[~,~]}},
description={Commutator},
sort={0}
}

\newglossaryentry{b0}
{ name={\ensuremath{\protect\vec{B}_0}},
description={Static magnetic field},
sort={B0}
}

\makeglossaries

\begin{document}
Text.\gls{div} \gls{commut} \gls{b0}

\printglossaries
\end{document}


Here, for example, the commutator do not appear in the glossary. I would like to have it appearing after the divergence operator despite their identical sorting keys.

I would suggest using \makenoidxglossaries together with \printnoidxglossary[sort=standard] which sorts the elements first according to the sort-key and then according to usage.

\documentclass{article}
\usepackage[nogroupskip]{glossaries}

\newglossaryentry{div}
{ name={\ensuremath{\protect\vec{\nabla}.}},
description={Divergence operator},
sort={0}
}

\newglossaryentry{commut}
{ name={\ensuremath{\protect[~,~]}},
description={Commutator},
sort={0}
}

\newglossaryentry{b0}
{ name={\ensuremath{\protect\vec{B}_0}},
description={Static magnetic field},
sort={B0}
}

\newglossaryentry{b1}
{ name={\ensuremath{\protect\vec{B}_1}},
description={Static magnetic field},
sort={B0}
}

\makenoidxglossaries

\begin{document}
Text.\gls{b1} \gls{div} \gls{commut} \gls{b0}

\printnoidxglossary[sort=standard]
\end{document}


Then you get

First the 0 entries show up according to usage, then the two B0 entries.

(xindy might change the entries for the sorting, so I'm not sure if it works there...)

• This actually works quite nicely indeed. No especial problem with xindy, apparently. I just would like to wait a little more if someone come with another answer, like something that adds a number at the end of the "sort" part or something.
– HcN
Feb 1, 2016 at 13:07
• +1 I hadn't noticed that as a by-product of the pre-sorted list being ordered according to usage this would happen! @HcN I think this is the best solution. The makeindex/xindy options set the sort value when the entry is defined. (This isn't simply the value of the sort key but it has also had the makeindex/xindy special characters escaped.) If the sort value is later adjusted it's liable to cause clones of the entries with the modified value which will confuse makeindex/xindy. The first use flag can't be relied on in this context as it may have been reset. Feb 2, 2016 at 13:31