33

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.

1 Answer 1

37

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*.

9
  • Werner, I have a quick question here. Whenever I try to bold the first word on the itemize, it automatically goes to the first in the sortedlist. Is there any way I can automatically change the first word to be bold ?
    – Jdbaba
    Commented Jun 28, 2013 at 15:10
  • 1
    @Jdbaba: See my updated post, which now provides \sortitem[<sort label>]{<label/description>}, allowing you to supply a different label to sort on from the description. This is very commonly done when sorting is involved, since you could use macros that don't have any relevant sorting reference.
    – Werner
    Commented Jun 28, 2013 at 15:41
  • @ Werner: thank you for this addition. This works perfect. I wish I could double vote.
    – Jdbaba
    Commented Jun 28, 2013 at 16:04
  • @Werner: This is an awesome idea (wish I had thought of it). But and having some difficulty in using it and can't quite figure out what is going on. With Sorted 2: \begin{sortedlist} \sortitem{foo} \sortitem{abc} \sortitem[yyx]{\textbf{yyx}} \end{sortedlist} I get two issues: 1. The title line appears as Sorted 2: x (not sure where the x is coming from) and 2. yyz appears first? The spacing betwen the title and the x depends on the number of \sortitems. Screenshot. Commented Dec 21, 2016 at 9:07
  • Seems that there is some issue with having an optional parameter in \sortitem. If I make it have two mandatory parameters (and adjust the definition), things work just fine. It seems that the x in the output seems to any characters after the first two yy in the optional parameter. So, using yyz4 as the first parameter, results in the title being Sorted 2: z4. You can simply replace [MVAR] in your example with [yyMVAR] to see this problem. Commented Dec 21, 2016 at 9:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .