# Biblatex: how to automatically sort citation by year (sortcites=ynt) when references are sorted by name (sorting=nyt)

I want to sort in-text citations by year, but bibliography by name. It sound exactly like this question.

However, suggested solution (using \usepackage[sorting=ynt]{biblatex} and \printbibliography[sorting=nty]) does not work for me (see MWEB below). Indeed, the list of references remains year-sorted instead of name sorted.

Question: How to force sorting=nty in \printbibliography?

\documentclass{article}
\usepackage[%
backend=biber,%
style=authoryear-comp,%
natbib=true,%
sorting=ynt%
]{biblatex}
\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@book{key2000,
author = {Author, A.},
year = {2000},
title = {Alphabetical fist \& Year last},
publisher = {Publisher},
}
@book{key1900,
author = {Boathor, B.},
year = {1900},
title = {Alphabetical last \& Year first},
publisher = {Publisher},
}
\end{filecontents}

\begin{document}
Citation should be year-sorted, even if \verb+\citep+ is random \citep{key2000, key1900}.% citation NOT year-sorted

\printbibliography[sorting=nyt]
\end{document}


P.S.: At the opposite, using

\begingroup
\newrefcontext[sorting=nyt]
\printbibliography
\endgroup


overrides the \usepackage[sorting=ynt]{biblatex} option (i.e. LoR is name-sorted, but in-line citations as well).

• At the moment, I'm afraid, it looks quite bleak. I hope someone comes up with a good answer. See Different citation order in text and bibliography using biblatex and latexmk, Multiple citations: citation order different to bibliography order. The problem is that if you use different sorting schemes the disambiguation letters might be off. – moewe Mar 29 '17 at 14:21
• The problem is that the sorting option has been moved from \printbibliography to so called 'refcontexts'. See Biblatex order of entries in a multi-citation. And the default is that all citations obey the refcontext they were last printed in the bibliography. So simply using a global sorting that contradicts the \printbibliography's refcontext's sorting does not work – moewe Mar 29 '17 at 14:24
• @moewe I indeed saw Biblatex order of entries in a multi-citation and started to use sortcites=false and manually sorting references within citation... but I wanted to be lazy and make (Bib)LaTeX do it for me! | Just to be sure to understand your previous comment: the sorting scheme chosen, is the one of the refcontext selected when printbibliography is called and not at the time of the \cite command, right? (I tried to add \newrefcontext[sorting=ynt] right after \begin{document} + what I added in my P.S, but it indeed doesn't work.) – ebo Mar 29 '17 at 14:32
• Indeed. Specifically it is the refcontext of the last appearance of the particularly entry in an \printbibliography. There are ways to manually assign the refcontext (i.e. override the 'use the refcontext of the last bibliography' rule). But you need to be careful about potentially different extrayears. – moewe Mar 29 '17 at 14:33
• Did my answer work for you even in your actual production document? – moewe Apr 6 '17 at 9:12

Use

\assignrefcontextentries[]{*}


with

\usepackage[%
backend=biber,%
style=authoryear-comp,%
natbib=true,%
sorting=ynt%
]{biblatex}


and then

\begin{document}
Citation should be year-sorted, even if \verb+\citep+ is random \citep{key2000, key1900}.% citation NOT year-sorted
\begin{refcontext}[sorting=nyt]
\printbibliography
\end{refcontext}
\end{document}


Some more explanation since this comes up more often.

biblatex does not allow the sorting option for \printbibliography any more. It was removed because it could lead to weird sorting results.

Instead, now you use 'refcontexts' to control sorting. A refcontext controls sorting, labelprefix and sortingnamekeytemplate and a few other things (possible more in the future).

An entry can appear in different refcontexts and any extra label data (extraeyar, extraalpha) will be recalculated based on the specific details (e.g. sorting) for each refcontext. This can lead to undesired behaviour as shown in the example below.

If you want to sort your bibliography nyt, you now need

\begin{refcontext}[sorting=nyt]
\printbibliography
\end{refcontext}


Now we only need to get the citations right! Here a fancy mechanism kicks in. By default entries are assigned to the last refsection of the \printbibliography where they were printed. In our case that would lead to all entries being assigned to the nyt context.

If you want to have the ynt context, we need to explicitly ask for that (i.e. override the automatic assignment). This can be done (amongst others) with \assignrefcontextentries. With \assignrefcontextentries[]{*} we simply assign all entries to the globally established refcontext, which in our case is ynt sorting.

Note that using different refcontexts (especially sortings) can lead to the same entry having different extrayear labels in citations and bibliography. This can really mess up your bibliography. Even though the chances are fairly slim that something like this happens in your current set-up, it might, and so I would advise against using different refcontext in this way.

Here is an admittedly contrived example that shows this behaviour with your set-up. It can be much easier to achieve such an effect with other pairings of sort schemes. The trick here was that nyt considers the volume for sorting while ynt does not.

\documentclass{article}
\usepackage[style=authoryear,sorting=ynt]{biblatex}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{one,
author = {Elk, Anne},
title  = {Title},
volume = {1},
note   = {sorts first in ynt},
}
@book{two,
author = {Elk, Anne},
title  = {Title},
note   = {sorts first in nyt},
}
\end{filecontents}

\begin{document}
\nocite{*}
\printbibliography[title={\refname{} (global sorting \texttt{ynt})}]
\newrefcontext[sorting=nyt]
\nocite{*}
\printbibliography[title={\refname{} (sorting \texttt{nyt})}]
\end{document


• Can you construct a situation in which the situation described in your last paragraph actually occurs? If I understand it correctly, two entries should only receive an extrayear, if they have the same authors and the same year. However, in this case the order is the same, both in nyt and ynt. Hence, the extrayear will not differ. – gerw Jul 21 '17 at 14:22
• @gerw See the edit, please. The example is contrived and uses a dependence on the volume field in one of the two schemes that might not have been immediately obvious, but it shows that the problem can be reproduced and is not purely theoretical. As mentioned in the answer, other pairings of schemes might be much more susceptible to these kind of entries. – moewe Jul 21 '17 at 15:00
• I see. However, if I add the (quite reasonable) assumption that no two titles are the same (at least with same authors and years), can something like this happen with nyt and ynt, too? – gerw Jul 21 '17 at 18:58
• @gerw In that case you are probably safe. At least I can't think of an example that would be problematic in that case off the top of my head. I would, however, not rely on things working out smoothly when it is possible to come up with problematic cases. Keep in mind that with other sort schemes different things might trigger the problematic behaviour. – moewe Jul 21 '17 at 19:07
• @bonanza As usual: You can have \newrefcontext or \begin{refcontext}...\end{refcontext} inside a refsection. Does that not work for you? If you have problems with this, I suggest you ask a new question with an MWE. – moewe Jul 25 at 6:57

A 'quick and dirty' solution (less good than moewe's one) is use biblatex's sortcites=false option, to enable the manual sorting of in-line citations.

\documentclass{article}
\usepackage[%
backend=biber,%
style=authoryear-comp,%
natbib=true,%
sorting=nyt,%
sortcites=false,
]{biblatex}
\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@book{A2000,
author = {Author, A.},
year = {2000},
title = {Alphabetical fist \& Year last},
publisher = {Publisher},
}
@book{B1900,
author = {Boathor, B.},
year = {1900},
title = {Alphabetical last \& Year first},
publisher = {Publisher},
}

@book{C1950,
author = {Coathor, C.},
year = {1950},
title = {Alphabetical last \& Year first},
publisher = {Publisher},
}
}
\end{filecontents}

\begin{document}
In-line citations should be manually sorted in the desired order, and the References list will be alphabetically sorted.

Example:
\begin{itemize}
\item This citation is manually year-sorted \citep{B1900, C1950, A2000},
\item this one is manually shuffled \citep{C1950, B1900, A2000}.
\end{itemize}

\printbibliography[]
\end{document}