# Sort Numbers After Their Values

Consider the numbers

2^4, 4^2, 3^2, and 5^2

Is it possible to make LaTeX calculate the values of these numbers and then arrange then automatically according to their values?

That is, in this particular case, I would like to get the output

3^2, 2^4, 4^2, and 5^2

P.S. It doesn't have to be a LaTeX 3 solution.

• Do you have a fixed amount of numbers you want to calculate and sort in this way or does it need to by dynamic, for any amount of numbers? – Big-Blue Jul 3 '16 at 14:25
• @Big-Blue It should be dynamic. (Sorry, I completely overlooked your question.) – Svend Tveskæg Jul 5 '16 at 10:16

Easy with xparse and expl3:

\documentclass{article}

\usepackage{xparse,expl3}

\ExplSyntaxOn
\NewDocumentCommand{\sortnumberlist}{m}
{
\clist_set:Nn \l_svend_numberlist_clist { #1 }
\clist_sort:Nn \l_svend_numberlist_clist
{
\fp_compare:nTF { ##1 > ##2 }
{
\sort_return_swapped:
}
{
\sort_return_same:
}
}
\clist_clear:N \l_svend_printnumberlist_clist
\clist_map_inline:Nn \l_svend_numberlist_clist
{
\clist_put_right:Nn \l_svend_printnumberlist_clist { $##1$ }
}
\clist_use:Nnnn \l_svend_printnumberlist_clist { ~and~ }{ ,~ }{ ,~and~ }
}
\clist_new:N \l_svend_printnumberlist_clist
\clist_new:N \l_svend_numberlist_clist
\ExplSyntaxOff

\begin{document}

\sortnumberlist{2^4, 4^2, 3^2, 5^2, 13, 25-1}

\end{document}


• Does someone know why l3sort isn’t documented in source3.pdf? – Tobi Jul 5 '16 at 10:27
• @Tobi It's still in the experimental branch. – egreg Jul 5 '16 at 10:30