[Tex/LaTex] Sorting bibliography according to the order in .bib file

bibliographiesbibtexsorting

Is it possible to sort the order of references using the order in the .bib file?

Best Answer

Here's a solution that should work with any bib file, including one in which the entries have been sorted in reverse alphabetical order.

First, insert the following instruction at the top of the bib file:

@preamble{ "\newcommand{\noop}[1]{}" }

At first blush, the \noop instruction would seem to be pointless as it doesn't do anything with its argument. While this is true for the LaTeX typesetting run, it is not true for the BibTeX sorting phase. In fact, for the sorting phase of its job, BibTeX will replace \noop{abc} with abc and consider "abc" to be a part of the material that needs to be sorted alphabetically.

Second, assuming you have fewer than 26*26=676 entries in the bib file, insert \noop{aa}, \noop{ab}, ..., \noop{az}, \noop{ba}, \noop{bb}, ... instructions immediately (without space) before the surname of the first author in the author field of each and every entry:

@article{xyz,
   author  = "Dane \noop{aa}Miller and Jane Smith",
   ...
}
@book{123,
   author = "Jill \noop{ab}Anderson and Bill Carlson",
   ...
}
...
@techreport{johnson:2012,
   author = "\noop{ac}Johnson, Jimmy",
   ...
}

(If you have more than 676 entries, just expand the string length of the argument of \noop suitably, e.g, to aaaa, aaab, etc.)

Third, be sure to use a bibliography style file that sorts the bib entries alphabetically. The point of the \noop instructions should now be clear: For sorting purposes, the relevant surnames are "aaMiller", "abAnderson", and "acJohnson", and hence the three entries will be sorted in the same way they occur in the bib file (rather than Anderson followed by Johnson followed by Miller). Later, when LaTeX does its typesetting thing, it discards the aa, ab, etc prefixes, and the names will be typeset correctly.