Sort subsections alphabetically

TeX is Turing complete language so I am sure the problem can be "easily" solved. However, this is best done (at least on Unix) with the standard Unix tools. Create a directory interfaces in which you would create a separate .tex file for the description of each interface. Something like

z-interface.tex
a-interface.tex
q-interface.tex
b-interface.tex

Now do something like

ls interfaces > interface-names.txt

which will create interface-names.txt file with interfaces sorted in alphabetical order. Using awk you can easily add \input TeX command in front of each file name. Like

awk '{print "\\input",$1}' interface-names.txt > interface-names.tex

Now just put the following line

\input interface-names.tex

into your main .tex file and you will have all sub sections in proper order as long as main .tex file and interface .tex files are in the same directory.


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.