I am attempting to alphabetically sort an array of elements which need to be expanded. I have used a modification of the sorting code in this answer, but I am seeing an unsorted list of 'Doga, Kittya1, Doga1,' where I expect the sorted, expanded elements: 'Doga, Doga1, Kittya1,'. Obviously I am doing something wrong as it is not even sorting according to the variable names. Additionally, I would like to identify the last iteration and not print the comma in this case. I was unable to an appropriate datatool variable.
\documentclass{article}
\usepackage{datatool}
\newcommand{\cat}[2][]{Kitty%
\ifthenelse{\equal{#1}{}}{}{%
\ifthenelse{\equal{#2}{}}{$_{\small{#1}}$}
{$_{\small{#1#2}}$}}%
}
\newcommand{\dog}[2][]{Dog%
\ifthenelse{\equal{#1}{}}{}{%
\ifthenelse{\equal{#2}{}}{$_{\small{#1}}$}
{$_{\small{#1#2}}$}}%
}
\def\CA{\cat[a]{1}}
\def\CB{\cat[b]{1}}
\def\DA{\dog[a]}
\def\DB{\dog[a]{1}}
\newcommand{\sortitem}[1]{%
\DTLnewrow{list}% Create a new entry
\DTLnewdbentry{list}{description}{#1}% Add entry as description
}
\newenvironment{sortedlist}{%
\DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
\DTLsort{description}{list}% Sort list
\DTLforeach*{list}{\theDesc=description}{%
\theDesc
, }% Print each item
}
\begin{document}
\begin{sortedlist}\sortitem{\DB}\sortitem{\CA}\sortitem{\DA}\end{sortedlist}
\end{document}
