In How to sort an alphanumeric list , there is a nice and short sort facility, which I am reproducing here:
%% from https://tex.stackexchange.com/questions/6988/how-to-sort-an-alphanumeric-list
\usepackage{etoolbox}
\makeatletter
% #1 - comparator
% #2 - token list to sort
\newcommand\sort[2]{%
\ifstrempty{#2}
{}% else
{%
\sort@begin#1{}#2\sort@s\sort@begin
}%
}
% helpers
\def\sort@s{\sort@s}
\def\ifsort@s#1{%
\ifx\sort@s#1%
\expandafter@firstoftwo
\else
\expandafter@secondoftwo
\fi
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - rest of the list
\def\sort@begin#1#2#3#4\sort@begin{%
\ifsort@s{#4}
{%
\sortend{#3}%
\sort#1{#2}%
}% else
{%
\sort@go#1{#2}{#3}#4\sort@go
}%
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - token under consideration
% #5 - rest of the list
\def\sort@go#1#2#3#4#5\sort@go{%
#1{#3}{#4}{\sort@output#1{#2}{#5}}%
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - rest of the list
% #4 - smaller of the two tokens
% #5 - larger of the two tokens
\def\sort@output#1#2#3#4#5{%
\ifsort@s{#3}
{%
\sortoutput{#4}%
\sort#1{#2{#5}}%
}% else
{%
\sort@begin#1{#2{#5}}{#4}#3\sort@begin
}%
}
\def\sort@numlt#1#2#3{%
\ifnumcomp{#1}<{#2}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\def\sort@numgt#1#2#3{%
\ifnumcomp{#1}>{#2}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\def\sort@alpha#1#2#3{%
\ifnumcomp{\pdfstrcmp{#1}{#2}}<{0}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\newcommand\sortnumlt{\sort\sort@numlt}
\newcommand\sortnumgt{\sort\sort@numgt}
\newcommand*\sortalpha{\sort\sort@alpha}
\makeatother
\newcommand\sortoutput[1]{#1 $\rightarrow$ }
\newcommand\sortend[1]{#1. $\leftarrow$ }
I stuck the above into a sort.sty file, and now I am trying to use it on a sample.
\documentclass{article}
\usepackage{sort}
\def\mystring{}
\makeatletter\def\addstring#1{\g@addto@macro\mystring{ #1}}\makeatother
\begin{document}
Sorted Literals: \sortalpha{{Goodbye, 1}{Cruel, 2}{World, 3}}
\bigskip
\addstring{goodbye, \pageref{pg:goodbye}.} \label{pg:goodbye}
\addstring{cruel, \pageref{pg:cruel}.} \label{pg:cruel}
\addstring{world, \pageref{pg:world}.} \label{pg:world}
Collected: \texttt{\mystring}
Sorted Collected: \sortalpha{\mystring}
\end{document}
The output is appropriately sorted for the literals, but not for the contents of the string. \expandafter before \mystring does not seem to do it, either.
I hope the magic to accomplish this on the contents of the macro is simple. help appreciated.
PS: I admit to wanting to create my own little sorted keyword index (with simple sequential running rather than more fancy formatting) at the end of my latex file without having to invoke an external program like \makeindex. I may also need to add a \MakeUppercase.


\expandafter\sortalpha\expandafter{\mystring}would be required to sort the contents of\mystring. Also, your\addstringdefinition should be\g@addto@macro\mystring{{#1}}}to mimic your literal case input. Finally, there seems to be an issue when doing what I say on the first pass, because\pagerefis still not sorted out. But once you can get the aux file written properly, what I suggest works. – Steven B. Segletes Nov 03 '21 at 02:26\pagerefis to a non-existent label (as it is on the first pass), latex blocks with! Undefined control sequence. \reserved@a ->\@nilat the\expandafter\sortalpha\expandafter{\mystring}. I had hoped that\pagerefwould just produce the standard '??' text on the first run and the correct page number on the second run, but instead the\expandaftercompletely blocks the first run. Easy fix? – ivo Welch Nov 03 '21 at 05:51\def\chkpageref#1{\@ifundefined{r@#1}{0}{\pageref{#1}}}and then\addstring{goodbye, \chkpageref{pg:goodbye}.}. – Marijn Nov 03 '21 at 09:27