Although LaTeX is not the best language for this sort of thing, here is a potential solution. It uses the pgffor
package.
First, we load the pgffor
package and define a counter that will keep track of how many items there are in our list.
\usepackage{pgffor}
\newcounter{SortListTotal}
Then, we define a command that will store the list items.
\newcommand{\sortitem}[2]{\expandafter\def\csname SortListItem#1\endcsname{#2}\stepcounter{SortListTotal}}
The first argument of \sortitem
is the item's number; the second is the item text.
Now, we define a command to print out the list. This command also resets the counter, ready for a new sorted list.
\newcommand{\printsortlist}{\foreach\currentlistitem in{1,2,...,\value{SortListTotal}}{\item[\currentlistitem]\csname SortListItem\currentlistitem\endcsname}\setcounter{SortListTotal}{0}}
These commands are used as in the following:
\begin{enumerate}
\sortitem{3}{This is the third item.}
\sortitem{4}{This is the fourth item.}
\sortitem{1}{This is the first item.}
\sortitem{2}{This is the second item.}
\printsortlist
\end{enumerate}
Alternatively, if you want to specify the order after storing the items:
\makeatletter
\newcounter{SortListTotal}
\newcommand{\sortitem}[1]{\stepcounter{SortListTotal}\expandafter\def\csname SortItem\arabic{SortListTotal}\endcsname{#1}}
\newcommand{\printsortlist}[1]{\@for\currentitem:=#1\do{\item\csname SortItem\currentitem\endcsname}\setcounter{SortListTotal}{0}}
\makeatother
These commands are used as in the following:
\begin{enumerate}
\sortitem{This is the third item.}
\sortitem{This is the fourth item.}
\sortitem{This is the first item.}
\sortitem{This is the second item.}
\printsortlist{3,4,1,2}
\end{enumerate}
You can use the label
, ref
keys from the enumitem
package to control the format for the labels and the references (I added some settings for the third and fourth nesting levels just as an illustration):
\documentclass{article}
\usepackage{enumitem}
\usepackage{cleveref}
\setlist[enumerate,1]{label=(\roman*),ref=(\roman*)}
\setlist[enumerate,2]{label=(\alph*),ref=(\roman{enumi}-\alph*)}
\setlist[enumerate,3]{label=(\Alph*),ref=(\roman{enumi}-\alph{enumii}-\Alph*)}
\setlist[enumerate,4]{label=(\arabic*),ref=(\roman{enumi}-\alph{enumii}-\Alph{enumiii}-\arabic*)}
\begin{document}
\begin{enumerate}
\item\label{fi} The first item
\item the second item
\begin{enumerate}
\item the first sub-item
\item\label{ssi} the second sub-item
\begin{enumerate}
\item\label{fssi} the first subsub-item
\begin{enumerate}
\item the first subsubsub-item
\item\label{ssssi} the second subsubsub-item
\end{enumerate}
\item the second subsub-item
\end{enumerate}
\end{enumerate}
\end{enumerate}
This is a fancy reference to the first item: \cref{fi}.
The second sub-item can be referred to as \cref{ssi}.
The second subsub-item can be referred to as \cref{fssi}.
The second subsubsub-item can be referred to as \cref{ssssi}.
\end{document}

Best Answer
Use enumitem package
Another solution is to use the enumerate package:
See daleif answer to learn more about advantages of enumitem over enumerate.
Note that if you're using the beamer class, you don't need to load any extra package (the enumerate package is actually automatically loaded):
As mentioned is the comments, enumitem is not compatible with the beamer class unless you use the [shortlabels] option, as explained here: Possible incompatibility with enumitem