# Different sorting schemes for different entry types?

I'm writing my own style and for most entry types the type field must be printed after the title, but for some entries it must be printed before. So for some entry types I need the type field to be sorted before the title field, and for some I need it to be sorted after. Is there a way to do that?

MWE

\begin{filecontents}{\jobname.bib}
@book{abook,
author = {Alfred A.},
title = {A Book},
type = {B Type}}
@book{anotherbook,
author = {Alfred A.},
title = {Another Book},
type = {A Type}}
@article{anarticle,
author = {Alfred A.},
title = {An Article},
type = {C Type}}
\end{filecontents}

\documentclass{article}

\usepackage[backend=biber, bibstyle=standard]{biblatex}

\DeclareBibliographyDriver{book}{
\printnames{author}
\newunit
\printfield{title}
\newunit
\printfield{type}
}

\DeclareBibliographyDriver{article}{
\printnames{author}
\newunit
\printfield{type}
\newunit
\printfield{title}
}

\DeclareSortingScheme{nty}{
\sort{
\field{presort}
}
\sort[final]{
\field{sortkey}
}
\sort{
\field{sortname}
\field{author}
\field{editor}
\field{sorttitle}
\field{title}
\field{subtitle}
}
\sort{
%\field{type}
\field{sorttitle}
\field{title}
\field{subtitle}
}
\sort{
\field{sortyear}
\field{year}
}
\sort{
\literal{0000}
}
}

\begin{document}
\nocite{*}
\printbibliography
\end{document}


This is what I get:

Since it's sorted by title before type, "An Article" comes before "Another Book", when it should actually come last, since it's type starts with a "C".

Now if I uncomment that row that says \field{type} I get this:

Since it's sorted by type before title, "Another Book" comes before "A Book", when the opposite should be true.

This answer suggests using the sortkey field, but in my case this won't work, since the author always comes first.

• So we know none of the unspecified answers work, but you don't think we might need to see your code in order to come up with an alternative that does? Or that links to the other questions, together with an explanation of the problem with their workarounds, might not make it a bit easier for people to approach the problem, perhaps building on an existing answer? – cfr May 4 '16 at 1:47
• @cfr Well, I honestly didn't think that was necessary, I was just hoping someone would point me in the right direction, but ok, I'll update my question, sorry. – dbmrq May 4 '16 at 3:09
• You can't do type-specific sorting schemes as this doesn't make much sense, you'd be comparing different fields in the same steps in the sorting algorithm. – PLK May 4 '16 at 3:53
• @PLK Ah, so there's no way to do what I need? It doesn't seem like such a crazy requirement. :( – dbmrq May 4 '16 at 4:03
• I know it's annoying to be asked but why do you want to do this? It looks like you want it to look neater but that's not something to be addressed by sorting. You could have reference lists split by type and sorted differently instead? – PLK May 4 '16 at 6:21

It's an odd requirement since you will have to mirror the formatting somehow before the sorting happens. You could do it with sourcemaps by appending things to a custom field in the right order. Here is something to get you going which makes your MWE work:

\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\pertype{book}
\step[fieldsource=title]
\step[fieldset=usera, origfieldval]
\step[fieldsource=type]
\step[fieldset=usera, origfieldval, append]
}
\map{
\pertype{article}
\step[fieldsource=type]
\step[fieldset=usera, origfieldval]
\step[fieldsource=title]
\step[fieldset=usera, origfieldval, append]
}
}
}


\DeclareSortingScheme{nty}{
\sort{
\field{presort}
}
\sort[final]{
\field{sortkey}
}
\sort{
\field{sortname}
\field{author}
\field{editor}
\field{sorttitle}
\field{title}
\field{subtitle}
}
\sort{
\field{usera}
}
\sort{
\field{sortyear}
\field{year}
}
\sort{

Although it's not really nty sorting any more and you should probably name it something else.