[Tex/LaTex] Sorting a manual bibliography alphabetically by their labels

bibliographiessorting

How to sort the bibliography alphabetically by their labels

\begin{the bibliography}{10}
\bibitem[Polchinski, 1984]{jpol}
Polchinski,~J.
\newblock{(1984)},
\newblock Renormalization group and effective lagrangians.
\newblock {\textbf{ Nuclear Physics B}}, 231:269--295.

\bibitem[Binney \textit{et al.}, 1992]{binney}
Binney,~J.~J., Dowrick,~N.~J.,  Fisher,~A.~J. and Newman,~M.~E.~J.
\newblock{(1992)},
\newblock \textbf{The Theory of critical phenomena: An introduction to the renormalization group}.
\newblock {Clarendon Press, Oxford, UK}.

\bibitem[Politzer, 1974]{politzer}
Politzer,~H.~D.
\newblock{(1974)},
\newblock Asymptotic freedom: An approach to strong interactions.
\newblock {\textbf{ Physics Reports}}, 14(4):129--180.
\end {the bibliography}

i.e. I need the order to be Binney then Polchinski then Politzer (in the above example)

Best Answer

Update the l3kernel and l3experimental packages. Here is some code that defines a sortbibliography environment, which (1) captures everything until \end{sortbibliography} (using the environ package, which you probably already have), (2) splits what it found (\BODY) into individual items (at every \bibitem), (3) removes whatever was before the first \bibitem (and makes sure that was empty), (4) sorts the items in lexicographic order, (5) typesets everything within the environment of your choice (here I used thebibliography, but you seem to have a the bibliography environment in your setup. This should be fast for up to a few thousand items, although I have not tested it extensively.

\documentclass{article}
\usepackage{environ}
\usepackage{expl3}
\ExplSyntaxOn
\seq_new:N \l_riad_bib_seq % bibliography, split at \bibitem commands
\tl_new:N \l_riad_pre_tl   % part before the first \bibitem
\prg_new_conditional:Npnn \riad_str_compare:nNn #1#2#3 { TF }
  {
    % Comparing two entries in the bibliography is done using
    % the primitive \pdfstrcmp (for XeTeX, use \strcmp, for LuaTeX, use
    % \pdf@strcmp from Oberdiek's package pdftexcmds), which compares
    % strings in lexicographic order.
    %
    \if_int_compare:w
        \pdfstrcmp { \exp_not:n {#1} } { \exp_not:n {#3} } #2 \c_zero
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }
\NewEnviron{sortbibliography}[2]
  {
    \begin{#1}{#2}
      \seq_set_split:NnV \l_riad_bib_seq { \bibitem } \BODY
      \seq_pop:NN \l_riad_bib_seq \l_riad_pre_tl
      \tl_remove_all:Nn \l_riad_pre_tl { \par } % Error checking: only \par and
      \tl_remove_all:Nn \l_riad_pre_tl { ~ }    % spaces before first \bibitem.
      \tl_if_empty:NF \l_riad_pre_tl            % Otherwise, complain about the
        {                                       % tokens found before \bibitem.
          \msg_error:nnxxx { riad } { junk-before }
            { \token_to_str:N \bibitem }
            { sortbibliography }
            { \tl_to_str:N \l_riad_pre_tl }
        }
      \seq_sort:Nn \l_riad_bib_seq
        {
          \riad_str_compare:nNnTF {##1} > {##2}
            { \sort_return_swapped: }
            { \sort_return_same: }
        }
      \seq_map_inline:Nn \l_riad_bib_seq { \bibitem ##1 }
    \end{#1}
  }
\msg_new:nnn { riad } { junk-before }
  { Extra~'#3'~before~the~first~'#1'~in~environment~'#2'. }
\ExplSyntaxOff
\begin{document}
\begin{sortbibliography}{thebibliography}{10}
\bibitem[Polchinski, 1984]{jpol}
Polchinski,~J.
\newblock{(1984)},
\newblock Renormalization group and effective lagrangians.
\newblock {\textbf{ Nuclear Physics B}}, 231:269--295.

\bibitem[Binney \textit{et al.}, 1992]{binney}
Binney,~J.~J., Dowrick,~N.~J.,  Fisher,~A.~J. and Newman,~M.~E.~J.
\newblock{(1992)},
\newblock \textbf{The Theory of critical phenomena: An introduction to the renormalization group}.
\newblock {Clarendon Press, Oxford, UK}.

\bibitem[Politzer, 1974]{politzer}
Politzer,~H.~D.
\newblock{(1974)},
\newblock Asymptotic freedom: An approach to strong interactions.
\newblock {\textbf{ Physics Reports}}, 14(4):129--180.
\end{sortbibliography}
\end{document}
Related Question