[Tex/LaTex] Ordering .bib file alphabetically

biberbiblatexbiblatex-chicagobibliographies

I'm searching for a way to edit/re-order the entries in the .bib file to match the ordering defined for the bibliography. There are several questions here about ordering the bibliography, but I haven't found anything regarding ordering the .bib file itself…perhaps because it isn't possible! For instance, Sorting bibliography according to the order in .bib file is the opposite of what I'm looking for. I want to sort the .bib file according to the order in the bibliography.

The reason is because I have several .bib files organized by topic, each topic file organized by when I read the source. It would save me quite a bit of time to have the entries organized so that, for instance, I could delete an entire stretch of one author's sources instead of searching for them one by one.

%LuaLaTeX
\documentclass{report}
\usepackage[authordate,backend=biber,sorting=nyt]{biblatex-chicago}

\begin{filecontents}{bib.bib}

@book{bcd,
    author          = {Mike Man},
    title           = {Title},
    year            = {2016},
    publisher       = {Books},
}

@book{cde,
    author          = {Bill Ban},
    title           = {Name},
    year            = {2009},
    publisher       = {Publisher},
}

@book{abc,
    author          = {Bill Ban},
    title           = {Something},
    year            = {2014},
    publisher       = {Somewhere},
}

\end{filecontents}

\addbibresource{bib.bib}
\nocite{*}

\begin{document}

Text.

\printbibliography

\end{document}

Ideally there could be a way to reorder bib.bib such that it matches the defined ordering in the bibliography (thus, authordate,sorting=nyt). In this case, the intended ordering should be cde, abc, bcd.

Best Answer

This is possible with biber's tool mode. Suppose your .bib is "test.bib" and you have the following in "test.conf" (this is just the config file version of the "nyt" sorting scheme - see the biblatex/biber PDF docs about this).

<config>
  <!-- nyt SORTING -->
  <sorting>
    <presort>mm</presort>
    <sort order="1">
      <sortitem order="1">presort</sortitem>
    </sort>
    <sort order="2" final="1">
      <sortitem order="1">sortkey</sortitem>
    </sort>
    <sort order="3">
      <sortitem order="1">sortname</sortitem>
      <sortitem order="2">author</sortitem>
      <sortitem order="3">editor</sortitem>
      <sortitem order="4">translator</sortitem>
      <sortitem order="5">sorttitle</sortitem>
      <sortitem order="6">title</sortitem>
    </sort>
    <sort order="4">
      <sortitem order="1">sortyear</sortitem>
      <sortitem order="2">year</sortitem>
    </sort>
    <sort order="5">
      <sortitem order="1">sorttitle</sortitem>
      <sortitem order="2">title</sortitem>
    </sort>
    <sort order="6">
      <sortitem order="1">volume</sortitem>
      <sortitem order="2">0</sortitem>
    </sort>
  </sorting>
</config>

Then simply run this command:

biber --tool --configfile=test.conf test.bib

and your sorted .bib will be in test_bibertool.bib.

biber uses a default config file in tool mode which you can find by running:

biber --tool-config

you can then copy this file and edit it to your needs. You can do many things to your .bib files with tool mode such as reformatting, sorting, changing the data using sourcemaps, materialising inheritance rules etc. See the biber PDF documentation.

The biber PDF manual (currently section 3.1.7) has details of the format of the sorting specification accepted in the config file.

Related Question