2

I try to sort enumerated lists in latex. All I found is how to sort items by their ordinal number (see Order items in enumerate environment automatically):

\documentclass{article}

\usepackage{pgffor}
\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

\begin{document}

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

\end{document}

Could you please help me how to rewrite \sortitem and \printsortlists such that I can use the following code:

\newcommand{\nc}{three,four,one,two}
\begin{enumerate}
\sortitem{one}{This is the third item.}
\sortitem{two}{This is the fourth item.}
\sortitem{three}{This is the first item.}
\sortitem{four}{This is the second item.}
\printsortlist{\nc}
\end{enumerate}

If it is not an easy task, I would also be happy if you provide an alternative solution to this problem.

Roloka
  • 203
  • Hm. You have now two variants, one with numbers and one with keywords. Which one do you really want? – Ulrike Fischer Dec 03 '18 at 12:44
  • I would like to use keywords rather than numbers. The code containing numbers is the example which I found in tex.stackexchange.com. – Roloka Dec 03 '18 at 12:46
  • 1
    I think it should be possible to combine https://tex.stackexchange.com/a/344002/ with your question, I will try it out and write an answer if it works. – Marijn Dec 03 '18 at 12:49

1 Answers1

2
\documentclass{article}

\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g__rol_sortlist_prop
\NewDocumentCommand\sortitem { m m }
 {
  \prop_gput:Nnn\g__rol_sortlist_prop { #1 }{ # 2}
 }

\NewDocumentCommand \printsortlist { m }
 {
  \clist_set:No \l_tmpa_clist { #1 }
  \clist_map_inline:Nn \l_tmpa_clist 
   {
    \item \prop_item:Nn \g__rol_sortlist_prop {##1} 
   }       
 }  
\ExplSyntaxOff

\begin{document}

\newcommand{\nc}{three,four,one,two}
\begin{enumerate}
\sortitem{one}{This is the third item.}
\sortitem{two}{This is the fourth item.}
\sortitem{three}{This is the first item.}
\sortitem{four}{This is the second item.}
\printsortlist{\nc}
\end{enumerate}

\end{document}

enter image description here

Ulrike Fischer
  • 327,261