[Tex/LaTex] Alphabetically display the items in itemize

glossariesitemizesorting

I am wondering whether it is possible to display the items alphabetically regardless of the order we type on the backend? For example I am using itemize as follows:

\begin{itemize}
    \item ISDYNSTP:  Is dynamic time step used?
    \item ISCDCA:
    \item MVAR
    \item IS2TL_
\end{itemize}

But I want to display the items in the alphabetical order. I don't know whether it is possible or not. I have like hundreds of itemize items. Actually I am trying to create a glossary for the different parameters.

Best Answer

Taking some code from How to sort an alphanumeric list, a mild change to your interface works for sorting via the datatool package:

enter image description here

\documentclass{article}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\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
  \begin{itemize}%
    \DTLforeach*{list}{\theDesc=description}{%
      \item \theDesc}% Print each item
  \end{itemize}%
}
\begin{document}

Default:
\begin{itemize}
  \item ISDYNSTP:  Is dynamic time step used ?
  \item ISCDCA:
  \item MVAR
  \item IS2TL
\end{itemize}

Sorted:
\begin{sortedlist}
  \sortitem{ISDYNSTP:  Is dynamic time step used ?}
  \sortitem{ISCDCA:}
  \sortitem{MVAR}
  \sortitem{IS2TL}
\end{sortedlist}

\end{document}

To allow for formatting of sorted elements, it's best to modify the syntax. To that end, the following MWE supplied an updated version of \sortitem[<sort label>]{<label/description>} that takes an optional <sort label> (used as the label to sort upon):

enter image description here

\documentclass{article}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\newcommand{\sortitem}[2][\relax]{%
  \DTLnewrow{list}% Create a new entry
  \ifx#1\relax
    \DTLnewdbentry{list}{sortlabel}{#2}% Add entry sortlabel (no optional argument)
  \else
    \DTLnewdbentry{list}{sortlabel}{#1}% Add entry sortlabel (optional argument)
  \fi%
  \DTLnewdbentry{list}{description}{#2}% Add entry description
}
\newenvironment{sortedlist}{%
  \DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
  \DTLsort{sortlabel}{list}% Sort list
  \begin{itemize}%
    \DTLforeach*{list}{\theDesc=description}{%
      \item \theDesc}% Print each item
  \end{itemize}%
}
\begin{document}

Default:
\begin{itemize}
  \item ISDYNSTP:  Is dynamic time step used ?
  \item ISCDCA:
  \item MVAR
  \item IS2TL
\end{itemize}

Sorted:
\begin{sortedlist}
  \sortitem{ISDYNSTP:  Is dynamic time step used ?}
  \sortitem[ISCDCA]{\textit{ISCDCA:}}
  \sortitem[MVAR]{\textbf{MVAR}}
  \sortitem{IS2TL}
\end{sortedlist}

\end{document}

If you want a case-insensitive comparison, just replace the \DTLsort command by \DTLsort*.

Related Question