# Biblatex/biber keep cycling and not settle when using custom sort

I'm trying to do a custom sort: citation order -> year/month -> author -> title

I think I got it in the style in the code below. However, when I run it: pdflatex -> biber -> pdflatex -> pdflatex. The numbers keep changing. First I got the compressed behavior in the citation, nicely [1]-[5], but the bibliography numbers are not in order. So, if I run pdflatex again, and now the bibliography is sorted, but the citation appears unsorted.

What am I doing wrong? Is there an option that I should turn on? or my sort has some problem?

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{ref2.bib}
@INPROCEEDINGS{C2009,
author = {C H.},
title = {M},
booktitle = {C},
year = {2009},
month = oct
}
@INPROCEEDINGS{J2010,
author = {J O.},
title = {L},
booktitle = {P},
year = {2010}
}
@ARTICLE{T2010,
author = {X B.},
title = {E},
year = {2010},
month = jun,
journal = {I}
}
@ARTICLE{Z2010,
author = {B},
title = {L},
year = {2010},
volume = {19},
month = feb,
journal = {I}
}
@ARTICLE{Z2007,
author = {B},
title = {H},
year = {2007},
month = jan,
journal = {I}
}

\end{filecontents}

\usepackage[backend=biber,style=ieee,bibencoding=utf8,defernumbers=true]{biblatex}

% sort in citation order and then by year/month, and author and title
\DeclareSortingScheme{cymat}{
\sort{\citeorder}
\sort{
\field{sortyear}
\field{year}
\literal{0}
}
\sort{
\field[padside=left,padwidth=2,padchar=0]{month}
\literal{0}
}
\sort[direction=descending]{
\field[padside=left,padwidth=2,padchar=0]{day}
\literal{99}
}
\sort{
\name{sortname}
\name{author}
\name{editor}
\name{translator}
\field{sorttitle}
\field{title}
}
}

\bibliography{ref2}

\pagestyle{empty}

\begin{document}
\cite{J2010, Z2010, Z2007, C2009, T2010}
\printbibliography[sorting=cymat]

\end{document}

-
The sorting option passed to \printbibliography controls only the order of the bibliography, not citations. Order of citations is specified by the package options sorting and sortcites. style=ieee sets sorting=none and sortcites=true. So load biblatex with sorting=cymat as Andrew suggests. Under biber 1.5, I don't get "cycling" numbers; the output stabilizes to the expected result you've shown on the right. – Audrey Mar 3 '13 at 18:21
@Audrey thanks for your comment. Using your idea and @Andrew's suggestion I manage to fix the problem. However, the defernumbers should be there because I need the sorting option for other bibliographies. – adn Mar 4 '13 at 2:59
That is fine. With a single bibliography, though, that option setting is superfluous. – Audrey Mar 4 '13 at 15:12

## 2 Answers

You should remove the defernumbers option, it delays the assignment of numbers and making the compressing of lists etc. problematic. Also the sorting option should be passed to the package, not the \printbibliography command.

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{ref2.bib}
@INPROCEEDINGS{C2009,
author = {C H.},
title = {M},
booktitle = {C},
year = {2009},
month = oct
}
@INPROCEEDINGS{J2010,
author = {J O.},
title = {L},
booktitle = {P},
year = {2010}
}
@ARTICLE{T2010,
author = {X B.},
title = {E},
year = {2010},
month = jun,
journal = {I}
}
@ARTICLE{Z2010,
author = {B},
title = {L},
year = {2010},
volume = {19},
month = feb,
journal = {I}
}
@ARTICLE{Z2007,
author = {B},
title = {H},
year = {2007},
month = jan,
journal = {I}
}

\end{filecontents}

\usepackage[backend=biber,style=ieee,bibencoding=utf8,sorting=cymat]{biblatex}

% sort in citation order and then by year/month, and author and title
\DeclareSortingScheme{cymat}{
\sort{\citeorder}
\sort{
\field{sortyear}
\field{year}
\literal{0}
}
\sort{
\field[padside=left,padwidth=2,padchar=0]{month}
\literal{0}
}
\sort[direction=descending]{
\field[padside=left,padwidth=2,padchar=0]{day}
\literal{99}
}
\sort{
\name{sortname}
\name{author}
\name{editor}
\name{translator}
\field{sorttitle}
\field{title}
}
}

\bibliography{ref2}

\pagestyle{empty}

\begin{document}
\cite{J2010, Z2010, Z2007, C2009, T2010}
\printbibliography

\end{document}

-
»the sorting option should be passed to the package, not the \printbibliography command« - Why is that? Despite the fact that it does not make much sense with a single bibliography it is officially supported with biber according to the biblatex manual. – clemens Mar 3 '13 at 17:11
It is not clear to me, but in the poster's situation it does not work as option to \printbibliography: i.e. the sorting/citing problem persists - it also issues a warning saying that you should use the defernumbers option in that case. There is some interaction problem in this example between the numeric-comp citation style and a sorting order that depends on citation order. – Andrew Swann Mar 3 '13 at 19:48
@cgnieder Please see my previous comment - sorry I forgot to flag you. – Andrew Swann Mar 3 '13 at 19:55
Audrey's comment to the question was very helpful. Thanks for your answer :) – clemens Mar 3 '13 at 20:16
Thanks @AndrewSwann for your solution. However, moving the sorting up to the package doesn't solve my problem because I have two bibliographies with different sorting orders. I read in the manual that is OK to have the sorting in the \printbibliography, but it is not working as you mention in your comment. Any other idea? – adn Mar 4 '13 at 2:03

Comments. Thanks to Andrew's and Audrey's comments I manage to fix the problem. However, for completeness I want to put the whole solution here. The issue is that the MWE didn't contain the extra bibliography as it wasn't needed to reproduce the problem. However, it is affected as an effect of the proposed solution. Thus, I'm presenting here a full solution that also addresses the problem when extra bibliographies are present.

## Full Solution

Although Andrew's solution fixes the problem by moving the sorting=cymat to the package loading options instead of using it with the \printbibliography macro, it doesn't address the problem of fixing the numbers when using other bibliographies.

However, I notice that the defernumbers=true option allows those numbers to be sorted correctly. Thus, the complete solution is to move the sorting option to the loading options and turn on the defernumbers.

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{ref2.bib}
@INPROCEEDINGS{C2009,
author = {C H.},
title = {M},
booktitle = {C},
year = {2009},
month = oct
}
@INPROCEEDINGS{J2010,
author = {J O.},
title = {L},
booktitle = {P},
year = {2010}
}
@ARTICLE{T2010,
author = {X B.},
title = {E},
year = {2010},
month = jun,
journal = {I}
}
@ARTICLE{Z2010,
author = {B},
title = {L},
year = {2010},
volume = {19},
month = feb,
journal = {I}
}
@ARTICLE{Z2007,
author = {B},
title = {H},
year = {2007},
month = jan,
journal = {I}
}
@ARTICLE{K2013,
author = {K O.},
title = {B},
year = {2013},
month = feb,
journal = {I},
keywords = {publication}
}
@INPROCEEDINGS{K2012,
author = {K  O.},
title = {S},
booktitle = {I},
year = {2012},
month = jul,
keywords = {publication}
}
@ARTICLE{M2011,
author = {M O.},
title = {M},
year = {2011},
month = jun,
journal = {L},
keywords = {publication}
}
@INPROCEEDINGS{M2010,
author = {M O.},
title = {S},
booktitle = {A},
year = {2010},
month = aug,
keywords = {publication}
}
@ARTICLE{M2012,
author = {M O.},
title = {S},
year = {2012},
month = jul,
journal = {I},
keywords = {publication}
}

\end{filecontents}

\usepackage[backend=biber,style=ieee,bibencoding=utf8,sorting=cymat,defernumbers=true]{biblatex}

\DeclareSortingScheme{ymdtn}{
\sort{
\field{presort}
}
\sort[final]{
\field{sortkey}
}
\sort[direction=descending]{
\field{sortyear}
\field{year}
\literal{9999}
}
\sort[direction=descending]{
\field[padside=left,padwidth=2,padchar=0]{month}
\literal{99}
}
\sort[direction=descending]{
\field[padside=left,padwidth=2,padchar=0]{day}
\literal{99}
}
\sort{
\field{sorttitle}
}
\sort[direction=descending]{
\field[padside=left,padwidth=4,padchar=0]{volume}
\literal{9999}
}
\sort{
\name{sortname}
\name{author}
\name{editor}
\name{translator}
\field{sorttitle}
\field{title}
}
}
% sort in citation order and then by year/month, and author and title
\DeclareSortingScheme{cymat}{
\sort{\citeorder}
\sort{
\field{sortyear}
\field{year}
\literal{0}
}
\sort{
\field[padside=left,padwidth=2,padchar=0]{month}
\literal{0}
}
\sort[direction=descending]{
\field[padside=left,padwidth=2,padchar=0]{day}
\literal{99}
}
\sort{
\name{sortname}
\name{author}
\name{editor}
\name{translator}
\field{sorttitle}
\field{title}
}
}

\bibliography{ref2}

\pagestyle{empty}

\begin{document}
\cite{J2010, Z2010, Z2007, C2009, T2010}
\printbibliography%[sorting=cymat] % removed the sorting from here to the package
\newrefsection
\nocite{*}
\printbibliography[resetnumbers=true,keyword=publication,type=article,heading=subbibliography,title={Journals},sorting=ymdtn]
\printbibliography[keyword=publication,type=inproceedings,heading=subbibliography,title={Conferences},sorting=ymdtn]

\end{document}


## Open Questions

However, why this is happening is beyond my knowledge. Why in the second bibliography the sorting option works in the \printbibliography while in the main one it doesn't? Perhaps this should be another question, or someone with more knowledge can expand on this regard.

-
The above does not stabilise for me - on the next pdflatex run the Journals get numbered [6], [10], [8], ... and the run after that they revert to the duplicate numbers [1], [2], [3],... I think you should ask a separate question about the multiple bibliography situation. – Andrew Swann Mar 4 '13 at 7:25
@AndrewSwann really? I got that number problem, but running pdflatex again stabilized them. I will ask another question, should I leave this answer here? Also, what version of biblatex/biber did you used? Maybe a version issue? – adn Mar 4 '13 at 7:28
Yes really, each pdflatex run toggles between the two results. This is why I said you had to remove the defnumbers option. I think it is fine to leave your answer here as the summary of the state of play. – Andrew Swann Mar 4 '13 at 7:33
Regarding your open question, please refer to my initial comment. Passing sorting=cymat to \printbibligraphy means that the citations will be sorted according to their cite order. With defernumbers enabled, label number assignment is deferred to \printbibligraphy. So the resulting label numbers will be ascending in the cymat order. Obviously this isn't the same as the cite order. So you won't get ascending or compressed label numbers in citation lists. – Audrey Mar 4 '13 at 15:22