# Sorting the bibliography by entry type

I am using BibTeX. For overall configuration I use a .cls file. I am looking for a way to sort the bibliography in this order: Books, articles, and miscellaneous. Currently all types of materials get sorted according to author name. As I am not using author parameter for misc, all misc categories appear first, which I don't want to happen.

-

Here's how to do it using biblatex. (The defernumbers package option ensures that the numeric labels are assigned according to the order of entries in the bibliographies.)

\documentclass{article}

\usepackage[defernumbers=true]{biblatex}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@misc{A01,
author = {Author, A.},
year = {2001},
title = {Alpha},
}
@book{B02,
author = {Buthor, B.},
year = {2002},
title = {Bravo},
location = {Location},
publisher = {Publisher},
}
@article{C03,
author = {Cuthor, C.},
year = {2003},
title = {Charlie},
journaltitle = {Journal title},
volume = {1},
}
\end{filecontents}

\nocite{*}

\begin{document}

\printbibliography[type=book,title={Books}]

\printbibliography[type=article,title={Articles}]

\printbibliography[nottype=book,nottype=article,title={Miscellaneous}]

\end{document}


(The filecontents environment is only used to include some external files directly into the example, so that it compiles. It is not necessary for the solution.)

-
If I read this correctly, this requires typing the bib file exactly in the order you want the items to appear? –  Willie Wong Dec 12 '10 at 16:51
@Willie: No, it doesn't. I modified the example to make that clear. –  lockstep Dec 12 '10 at 17:01
I'm using bibtex. I'm using a standard template that I found here: maths.ox.ac.uk/help/faqs/latex/bibtex So your solution didn't work for me. –  nixnotwin Dec 12 '10 at 17:49
@nixnotwin: Your link points to an introduction to bibtex and some of the most popular bst-styles. At the moment, biblatex also uses bibtex by default, but switching to biblatex may necessitate finetuning of the new (built-in) biblatex styles. What \bibliographystyle do you use at the moment? –  lockstep Dec 12 '10 at 19:18
Thanks for the clarification. Now it is perfectly clear. –  Willie Wong Dec 12 '10 at 20:35

You need to change the sort keys in the bst file in order to achieve this. Suppose you use bibliography style alpha. Then copy alpha.bst (you can locate that on your system by kpsewhich alpha.bst) into your tex directory and rename it to something like myalpha.bst. Inside that file locate the function presort and at the very end of that function (right before the }) insert the following code:

type$"book" = { "01-" sort.key$ *
'sort.key$:= } { type$ "article" =
{
"02-" sort.key$* 'sort.key$ :=
}
{
"99-" sort.key$* 'sort.key$ :=
}
if$} if$


This pretty strange piece of code prefixes the sort keys for books with 01-, for articles with 02- and for all the rest with 99-.

Note: If you wonder what all of that means: bibtex uses a stack based language inside the bst files, so you first push the arguments and then push the operator or function which pops its arguments from the top of the stacks, computes its result and then pushes the result on the stack. E.g. type$"book" = pushes the contents of the type field on the stack, then the string "book" and at last the comparison operator "=". If both are equal "=" will push true on the stack, or false otherwise. Then with the {} two code blocks are pushed on the stack and then $if pops the boolean value and the two code blocks and executes one of them, depending on the boolean value. So, you can decipher it after some time, but I think it'll never get really readable. (Actually, it's easier to write than to read IMO).

-
Very helpful. I tried to make a code with three levels and I have not figured out how to do it correctly. Could you add a code with three levels please? –  goLK Sep 7 '13 at 22:54
Hi goLK, maybe you post your question as a separate question, and specify exactly on which fields you want to sort. It would be easier to answer then, instead of cramming this into the answer above. –  Elmar Zander Sep 23 '13 at 6:33