4

Possible Duplicate:
How to sort an alphanumeric list

For example, I have the following code:

\begin{enumerate}
  \item Rabbit
  \item Apple
  \item Serpent
\end{enumerate}

Is there a way to add something to this and get a sorted, numbered list as output?

Rogach
  • 3,088
  • Related question: http://tex.stackexchange.com/questions/6988/how-to-sort-an-alphanumeric-list There the issue is solved in multiple different ways. – Vairis Apr 23 '12 at 09:28

1 Answers1

3

You can use a comma delimited list and then sort it. Here is a MWE.

\documentclass{article}
\usepackage{lstdoc,booktabs}
\makeatletter
\let\alist\@empty

\def\addtolist#1{%
    \lst@lAddTo\alist{#1,}
    \lst@BubbleSort{\alist} 
}

\parindent0pt
\newcounter{cnt}
\setcounter{cnt}{0}
\begin{document}


%% adding the data now
\addtolist{banana}  
\addtolist{zero}
\addtolist{apple}  
\addtolist{arab}

\@for\next:=\alist\do{%
   \stepcounter{cnt}%
   \thecnt\space \next\\
}
\end{document}

You can format the output within the \@for loop.

David Carlisle
  • 757,742
yannisl
  • 117,160
  • It looks good. But it prints an empty line at the end ("5") for me. And will it be possible to make several sorted lists in one document? – Rogach Apr 23 '12 at 10:31
  • @Rogach Thanks, will add a more complete solution late in the evening. Anything is possible in TeX. – yannisl Apr 23 '12 at 11:04