2

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}
Ann
  • 529

1 Answers1

2

Ifthenelse does not work inside the environment of \DTLnewdbentry and I change your code using \ifx. Also I had to add: \dtlexpandnewvalue before the database entry to let the commands and definitions expand before stored. Also in dogA you are giving the optional argument instead of the mandatory and that mistake makes its definition to ask for next argument and take the , after the expansion. A gave an empty mandatory.... but I suppose I could just replace [] with {}. Finally, \small does not take effect inside math mode. (Search for the way of "latex change math arguments".).

The code is here:

\documentclass{article}
\usepackage{datatool}
\usepackage{tikz}
\usepackage{ifthen}
\newcommand{\cat}[2][]{Kitty%
\ifx#1\empty\relax\else
\ifx#2\empty\small{$_{#1}$}\else\small{$_{#1#2}$}\fi\fi%
}
\newcommand{\dog}[2][]{Dog%
\ifx#1\empty\relax\else
\ifx#2\empty\small{$_{#1}$}\else\small{$_{#1#2}$}\fi\fi%
}

\def\CA{\cat[a]{1}}
\def\CB{\cat[b]{1}}
\def\DA{\dog[a]{}}
\def\DB{\dog[a]{1}}

\newcommand{\sortitem}[1]{%
   \DTLnewrow{list}%
   \dtlexpandnewvalue
   \DTLnewdbentry{list}{description}{#1}%
}

\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}



Unsorted:

\DB,\CA,\DA,\CB

Sorted:


\begin{sortedlist}
\foreach \i in {\DB,\CA,\DA,\CB}{\sortitem{\i}}
\end{sortedlist}
\end{document}

And the result:

enter image description here

I have linked : Sorting data in datatool depending on argument here and there was one more similar question that I was explaining the above about the \ifthenelse but it is probably deleted. So these two questions gives a whole answer. (May be it is somehow a duplicate but it was duplicate with the deleted because the linked is about \random that fails too).

koleygr
  • 20,105