[Tex/LaTex] Sort subsections alphabetically

sectioningsorting

We need to make a paper that describes a list of interfaces. The assignment however says we need to sort the interfaces alphabetically. Because this list is quite long (20+) I am looking for a way to sort the subsections (each subsection describes an interface) alphabetically. Without having to move code myself.

Is there a package that can handle this?

Here is a sample of the code:

\newcommand{\interfaceItem}[5]{
   \subsection{#1}\label{interfaceItem:#1}
   \paragraph{Paragraph 1}#2
   \paragraph{Paragraph 2}#3
   \paragraph{Paragraph 3}#4
   \paragraph{Paragraph 4}#5
}

Best Answer

Here's an implementation:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\interfaceItem}{mmmmm}
 {
  \seq_put_right:Nn \l_commusoft_interfaces_seq {#1}
  \cs_new:cpn { commusoft_interface_#1: } {
    \subsection{#1}\label{interfaceItem:#1}
    \paragraph{Paragraph 1}#2
    \paragraph{Paragraph 2}#3
    \paragraph{Paragraph 3}#4
    \paragraph{Paragraph 4}#5
  }
 }
\NewDocumentCommand{\printInterfaces}{ }
 {
  \seq_sort:Nn \l_commusoft_interfaces_seq
   {
    \string_compare:nnnTF {##1} {>} {##2} {\sort_return_swapped:} {\sort_return_same:}
   }
  \seq_map_inline:Nn \l_commusoft_interfaces_seq { \use:c { commusoft_interface_##1: } }
 }
\seq_new:N \l_commusoft_interfaces_seq
\prg_new_conditional:Npnn \string_compare:nnn #1 #2 #3 {TF}
  {
   \if_int_compare:w \pdftex_strcmp:D {#1}{#3} #2 \c_zero
    \prg_return_true:
   \else:
    \prg_return_false:
   \fi
  }
\ExplSyntaxOff

\interfaceItem{A}{A1}{A2}{A3}{A4}
\interfaceItem{C}{C1}{C2}{C3}{C4}
\interfaceItem{B}{B1}{B2}{B3}{B4}

\begin{document}

\printInterfaces

\end{document}

However, a simpler strategy can be easier:

\makeatletter
\newcommand{\interfaceItem}[5]{
   \@namedef{interface@\detokenize{#1}}{%
     \subsection{#1}\label{interfaceItem:#1}
     \paragraph{Paragraph 1}#2
     \paragraph{Paragraph 2}#3
     \paragraph{Paragraph 3}#4
     \paragraph{Paragraph 4}#5
    }
}
\newcommand{\printInterface}[1]{%
  \@nameuse{interface@\detokenize{#1}}%
}
\makeatother

You define your interfaces as before, in the preamble,

\interfaceItem{A}{A1}{A2}{A3}{A4}
\interfaceItem{C}{C1}{C2}{C3}{C4}
\interfaceItem{B}{B1}{B2}{B3}{B4}

and then say

\printinterface{A}

\printinterface{B}

\printinterface{C}

Sorting a list of short commands is easier than sorting big chunks of code.

Related Question