Sorting a manual bibliography alphabetically by their labels












4














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)










share|improve this question




















  • 2




    Welcome to TeX.sx! If you write the bibliography directly, you are responsible for the order; LaTeX won't change it.
    – egreg
    May 4 '13 at 17:38






  • 2




    I'm afraid the answer is: swap your bibitem's around manually :)
    – jubobs
    May 4 '13 at 17:39










  • It is very hard job
    – Riad
    May 4 '13 at 17:41










  • I have over 100 references, what can I do?
    – Riad
    May 4 '13 at 17:48






  • 1




    Apply the insertion-sort algorithm.
    – jubobs
    May 4 '13 at 17:52


















4














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)










share|improve this question




















  • 2




    Welcome to TeX.sx! If you write the bibliography directly, you are responsible for the order; LaTeX won't change it.
    – egreg
    May 4 '13 at 17:38






  • 2




    I'm afraid the answer is: swap your bibitem's around manually :)
    – jubobs
    May 4 '13 at 17:39










  • It is very hard job
    – Riad
    May 4 '13 at 17:41










  • I have over 100 references, what can I do?
    – Riad
    May 4 '13 at 17:48






  • 1




    Apply the insertion-sort algorithm.
    – jubobs
    May 4 '13 at 17:52
















4












4








4


1





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)










share|improve this question















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)







bibliographies sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 5 '13 at 15:36









Nils L

7,80212775




7,80212775










asked May 4 '13 at 17:35









Riad

2113




2113








  • 2




    Welcome to TeX.sx! If you write the bibliography directly, you are responsible for the order; LaTeX won't change it.
    – egreg
    May 4 '13 at 17:38






  • 2




    I'm afraid the answer is: swap your bibitem's around manually :)
    – jubobs
    May 4 '13 at 17:39










  • It is very hard job
    – Riad
    May 4 '13 at 17:41










  • I have over 100 references, what can I do?
    – Riad
    May 4 '13 at 17:48






  • 1




    Apply the insertion-sort algorithm.
    – jubobs
    May 4 '13 at 17:52
















  • 2




    Welcome to TeX.sx! If you write the bibliography directly, you are responsible for the order; LaTeX won't change it.
    – egreg
    May 4 '13 at 17:38






  • 2




    I'm afraid the answer is: swap your bibitem's around manually :)
    – jubobs
    May 4 '13 at 17:39










  • It is very hard job
    – Riad
    May 4 '13 at 17:41










  • I have over 100 references, what can I do?
    – Riad
    May 4 '13 at 17:48






  • 1




    Apply the insertion-sort algorithm.
    – jubobs
    May 4 '13 at 17:52










2




2




Welcome to TeX.sx! If you write the bibliography directly, you are responsible for the order; LaTeX won't change it.
– egreg
May 4 '13 at 17:38




Welcome to TeX.sx! If you write the bibliography directly, you are responsible for the order; LaTeX won't change it.
– egreg
May 4 '13 at 17:38




2




2




I'm afraid the answer is: swap your bibitem's around manually :)
– jubobs
May 4 '13 at 17:39




I'm afraid the answer is: swap your bibitem's around manually :)
– jubobs
May 4 '13 at 17:39












It is very hard job
– Riad
May 4 '13 at 17:41




It is very hard job
– Riad
May 4 '13 at 17:41












I have over 100 references, what can I do?
– Riad
May 4 '13 at 17:48




I have over 100 references, what can I do?
– Riad
May 4 '13 at 17:48




1




1




Apply the insertion-sort algorithm.
– jubobs
May 4 '13 at 17:52






Apply the insertion-sort algorithm.
– jubobs
May 4 '13 at 17:52












2 Answers
2






active

oldest

votes


















11














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_reversed: }
{ sort_ordered: }
}
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}





share|improve this answer



















  • 1




    Wow! awesome code, Bruno! :)
    – Paulo Cereda
    May 18 '13 at 13:10










  • I defined a similar string_compare:nnn(TF) conditional in this answer. By the way, pdftex_strcmp:D adapts itself to the engine used.
    – egreg
    May 18 '13 at 13:15












  • @egreg: I know pdf_strcmp:D adapts, but I am wary of suggesting it to users, since it is a Do not use command. I don't really want to provide a sort_lexicographic wrapper in l3sort because the proper approach would need to handle locales, which I am not ready to do :(.
    – Bruno Le Floch
    May 18 '13 at 13:55










  • Maybe a string_compare_ascii:nnn(TF) function might be handy for simple tasks.
    – egreg
    May 18 '13 at 13:58



















3














You're in trouble here. You've decided to create a manual bibliography rather than use bib(la)tex -- which might be justified in certain circumstances, but probaly the most fundamental feature of a manual bibliography is that the sorting is going to be manual as well ...since there's simply no tool involved that would be capable of doing the sorting for you.



One option that might help you is the one suggested by Jubobs. A second one is to give your thebibliography a format that can be processed by some third-party tool that's capable of sorting plain-text lines: such as any spreadsheet application, or, if you're on Windows, WinSorter (a nice multi-purpose tool for all kinds of text manipulation), or maybe even the TeX editor you're already using (e.g. WinEdt). After choosing one such a tool, try the following procedure:



(1) use a search and replace to remove everything that precedes the strings that you want your bib sorted by. I guess in your case this would be: bibitem[ -- so the lines now start with Binney etc. (RegExs might be helpful or necessary here).



(2) use a 2nd search and replace to remove all the line breaks that occur within an item, replacing them with some character or string that's not used otherwise, such as -- so that every bib item is conflated into one single line.



Before:



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


After:



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


(3) Use the sorting tool of your choice to sort the lines.



(4) Use the reverse of steps 2 and 1 to put your bibliography back into its original format.



(5) Consider using an automated bibliography with biblatex for your next paper :)






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "85"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f112432%2fsorting-a-manual-bibliography-alphabetically-by-their-labels%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    11














    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_reversed: }
    { sort_ordered: }
    }
    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}





    share|improve this answer



















    • 1




      Wow! awesome code, Bruno! :)
      – Paulo Cereda
      May 18 '13 at 13:10










    • I defined a similar string_compare:nnn(TF) conditional in this answer. By the way, pdftex_strcmp:D adapts itself to the engine used.
      – egreg
      May 18 '13 at 13:15












    • @egreg: I know pdf_strcmp:D adapts, but I am wary of suggesting it to users, since it is a Do not use command. I don't really want to provide a sort_lexicographic wrapper in l3sort because the proper approach would need to handle locales, which I am not ready to do :(.
      – Bruno Le Floch
      May 18 '13 at 13:55










    • Maybe a string_compare_ascii:nnn(TF) function might be handy for simple tasks.
      – egreg
      May 18 '13 at 13:58
















    11














    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_reversed: }
    { sort_ordered: }
    }
    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}





    share|improve this answer



















    • 1




      Wow! awesome code, Bruno! :)
      – Paulo Cereda
      May 18 '13 at 13:10










    • I defined a similar string_compare:nnn(TF) conditional in this answer. By the way, pdftex_strcmp:D adapts itself to the engine used.
      – egreg
      May 18 '13 at 13:15












    • @egreg: I know pdf_strcmp:D adapts, but I am wary of suggesting it to users, since it is a Do not use command. I don't really want to provide a sort_lexicographic wrapper in l3sort because the proper approach would need to handle locales, which I am not ready to do :(.
      – Bruno Le Floch
      May 18 '13 at 13:55










    • Maybe a string_compare_ascii:nnn(TF) function might be handy for simple tasks.
      – egreg
      May 18 '13 at 13:58














    11












    11








    11






    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_reversed: }
    { sort_ordered: }
    }
    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}





    share|improve this 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_reversed: }
    { sort_ordered: }
    }
    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}






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 8 mins ago









    tambre

    1457




    1457










    answered May 18 '13 at 13:04









    Bruno Le Floch

    33.8k5113211




    33.8k5113211








    • 1




      Wow! awesome code, Bruno! :)
      – Paulo Cereda
      May 18 '13 at 13:10










    • I defined a similar string_compare:nnn(TF) conditional in this answer. By the way, pdftex_strcmp:D adapts itself to the engine used.
      – egreg
      May 18 '13 at 13:15












    • @egreg: I know pdf_strcmp:D adapts, but I am wary of suggesting it to users, since it is a Do not use command. I don't really want to provide a sort_lexicographic wrapper in l3sort because the proper approach would need to handle locales, which I am not ready to do :(.
      – Bruno Le Floch
      May 18 '13 at 13:55










    • Maybe a string_compare_ascii:nnn(TF) function might be handy for simple tasks.
      – egreg
      May 18 '13 at 13:58














    • 1




      Wow! awesome code, Bruno! :)
      – Paulo Cereda
      May 18 '13 at 13:10










    • I defined a similar string_compare:nnn(TF) conditional in this answer. By the way, pdftex_strcmp:D adapts itself to the engine used.
      – egreg
      May 18 '13 at 13:15












    • @egreg: I know pdf_strcmp:D adapts, but I am wary of suggesting it to users, since it is a Do not use command. I don't really want to provide a sort_lexicographic wrapper in l3sort because the proper approach would need to handle locales, which I am not ready to do :(.
      – Bruno Le Floch
      May 18 '13 at 13:55










    • Maybe a string_compare_ascii:nnn(TF) function might be handy for simple tasks.
      – egreg
      May 18 '13 at 13:58








    1




    1




    Wow! awesome code, Bruno! :)
    – Paulo Cereda
    May 18 '13 at 13:10




    Wow! awesome code, Bruno! :)
    – Paulo Cereda
    May 18 '13 at 13:10












    I defined a similar string_compare:nnn(TF) conditional in this answer. By the way, pdftex_strcmp:D adapts itself to the engine used.
    – egreg
    May 18 '13 at 13:15






    I defined a similar string_compare:nnn(TF) conditional in this answer. By the way, pdftex_strcmp:D adapts itself to the engine used.
    – egreg
    May 18 '13 at 13:15














    @egreg: I know pdf_strcmp:D adapts, but I am wary of suggesting it to users, since it is a Do not use command. I don't really want to provide a sort_lexicographic wrapper in l3sort because the proper approach would need to handle locales, which I am not ready to do :(.
    – Bruno Le Floch
    May 18 '13 at 13:55




    @egreg: I know pdf_strcmp:D adapts, but I am wary of suggesting it to users, since it is a Do not use command. I don't really want to provide a sort_lexicographic wrapper in l3sort because the proper approach would need to handle locales, which I am not ready to do :(.
    – Bruno Le Floch
    May 18 '13 at 13:55












    Maybe a string_compare_ascii:nnn(TF) function might be handy for simple tasks.
    – egreg
    May 18 '13 at 13:58




    Maybe a string_compare_ascii:nnn(TF) function might be handy for simple tasks.
    – egreg
    May 18 '13 at 13:58











    3














    You're in trouble here. You've decided to create a manual bibliography rather than use bib(la)tex -- which might be justified in certain circumstances, but probaly the most fundamental feature of a manual bibliography is that the sorting is going to be manual as well ...since there's simply no tool involved that would be capable of doing the sorting for you.



    One option that might help you is the one suggested by Jubobs. A second one is to give your thebibliography a format that can be processed by some third-party tool that's capable of sorting plain-text lines: such as any spreadsheet application, or, if you're on Windows, WinSorter (a nice multi-purpose tool for all kinds of text manipulation), or maybe even the TeX editor you're already using (e.g. WinEdt). After choosing one such a tool, try the following procedure:



    (1) use a search and replace to remove everything that precedes the strings that you want your bib sorted by. I guess in your case this would be: bibitem[ -- so the lines now start with Binney etc. (RegExs might be helpful or necessary here).



    (2) use a 2nd search and replace to remove all the line breaks that occur within an item, replacing them with some character or string that's not used otherwise, such as -- so that every bib item is conflated into one single line.



    Before:



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


    After:



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


    (3) Use the sorting tool of your choice to sort the lines.



    (4) Use the reverse of steps 2 and 1 to put your bibliography back into its original format.



    (5) Consider using an automated bibliography with biblatex for your next paper :)






    share|improve this answer




























      3














      You're in trouble here. You've decided to create a manual bibliography rather than use bib(la)tex -- which might be justified in certain circumstances, but probaly the most fundamental feature of a manual bibliography is that the sorting is going to be manual as well ...since there's simply no tool involved that would be capable of doing the sorting for you.



      One option that might help you is the one suggested by Jubobs. A second one is to give your thebibliography a format that can be processed by some third-party tool that's capable of sorting plain-text lines: such as any spreadsheet application, or, if you're on Windows, WinSorter (a nice multi-purpose tool for all kinds of text manipulation), or maybe even the TeX editor you're already using (e.g. WinEdt). After choosing one such a tool, try the following procedure:



      (1) use a search and replace to remove everything that precedes the strings that you want your bib sorted by. I guess in your case this would be: bibitem[ -- so the lines now start with Binney etc. (RegExs might be helpful or necessary here).



      (2) use a 2nd search and replace to remove all the line breaks that occur within an item, replacing them with some character or string that's not used otherwise, such as -- so that every bib item is conflated into one single line.



      Before:



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


      After:



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


      (3) Use the sorting tool of your choice to sort the lines.



      (4) Use the reverse of steps 2 and 1 to put your bibliography back into its original format.



      (5) Consider using an automated bibliography with biblatex for your next paper :)






      share|improve this answer


























        3












        3








        3






        You're in trouble here. You've decided to create a manual bibliography rather than use bib(la)tex -- which might be justified in certain circumstances, but probaly the most fundamental feature of a manual bibliography is that the sorting is going to be manual as well ...since there's simply no tool involved that would be capable of doing the sorting for you.



        One option that might help you is the one suggested by Jubobs. A second one is to give your thebibliography a format that can be processed by some third-party tool that's capable of sorting plain-text lines: such as any spreadsheet application, or, if you're on Windows, WinSorter (a nice multi-purpose tool for all kinds of text manipulation), or maybe even the TeX editor you're already using (e.g. WinEdt). After choosing one such a tool, try the following procedure:



        (1) use a search and replace to remove everything that precedes the strings that you want your bib sorted by. I guess in your case this would be: bibitem[ -- so the lines now start with Binney etc. (RegExs might be helpful or necessary here).



        (2) use a 2nd search and replace to remove all the line breaks that occur within an item, replacing them with some character or string that's not used otherwise, such as -- so that every bib item is conflated into one single line.



        Before:



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


        After:



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


        (3) Use the sorting tool of your choice to sort the lines.



        (4) Use the reverse of steps 2 and 1 to put your bibliography back into its original format.



        (5) Consider using an automated bibliography with biblatex for your next paper :)






        share|improve this answer














        You're in trouble here. You've decided to create a manual bibliography rather than use bib(la)tex -- which might be justified in certain circumstances, but probaly the most fundamental feature of a manual bibliography is that the sorting is going to be manual as well ...since there's simply no tool involved that would be capable of doing the sorting for you.



        One option that might help you is the one suggested by Jubobs. A second one is to give your thebibliography a format that can be processed by some third-party tool that's capable of sorting plain-text lines: such as any spreadsheet application, or, if you're on Windows, WinSorter (a nice multi-purpose tool for all kinds of text manipulation), or maybe even the TeX editor you're already using (e.g. WinEdt). After choosing one such a tool, try the following procedure:



        (1) use a search and replace to remove everything that precedes the strings that you want your bib sorted by. I guess in your case this would be: bibitem[ -- so the lines now start with Binney etc. (RegExs might be helpful or necessary here).



        (2) use a 2nd search and replace to remove all the line breaks that occur within an item, replacing them with some character or string that's not used otherwise, such as -- so that every bib item is conflated into one single line.



        Before:



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


        After:



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


        (3) Use the sorting tool of your choice to sort the lines.



        (4) Use the reverse of steps 2 and 1 to put your bibliography back into its original format.



        (5) Consider using an automated bibliography with biblatex for your next paper :)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 13 '17 at 12:34









        Community

        1




        1










        answered May 5 '13 at 15:31









        Nils L

        7,80212775




        7,80212775






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f112432%2fsorting-a-manual-bibliography-alphabetically-by-their-labels%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks