# Grouping sorted bibliographyies by langid

I know I can group bibliographyes by keyword. But this is not a case, becouse if i dont use alphabetical sorting and numeric style I can split bibliography by keywords. But if I want sorted bibliography and numeric numbering and will try to split it I will break numbering. What I need is to have bib items of some language before than another language items. Example:

\documentclass[a4paper]{article}
\usepackage{fontspec}
\setmainfont[
BoldFont={DeJavu Serif Bold},
ItalicFont={DeJavu Serif Italic},
BoldItalicFont={DeJavu Serif BoldItalic}
]{DeJavu Serif}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@Book{AlKarapan,
author = {Καραπαναγοπούλου, Αλέξανδρος Κ.},
title = {Η Μεγάλη Σύνοδος της Ορθοδόξου Ανατολικής Εκκλησίας},
publisher = {auto-édition},
date = {1990/1998},
volumes = {6},
langid={greek},
}
@Inbook{CypinIstorijaRPC,
author = {Владислав Цыпин},
title = {История Русской Церкви 1917--1997},
chapter = {VIII},
publisher = {Издательство Спасо-Преображенского Валаамского монастыря},
date = {1997},
urldate = {2010-10-25},
url = {http://old.pravoslavie.by/podpod.asp?id=135&Session=10},
langid={russian},
}
@Inbook{authorrus,
author = {Другой Автор},
title = {Название},
chapter = {VIII},
publisher = {Издательство},
date = {1997},
urldate = {2010-10-25},
langid={russian},
}
@article{baez/article,
author       = {Baez, John C. and Lauda, Aaron D.},
title        = {Higher-Dimensional Algebra {V}: 2-Groups},
journaltitle = {Theory and Applications of Categories},
date         = 2004,
volume       = 12,
pages        = {423-491},
version      = 3,
eprint       = {math/0307200v3},
eprinttype   = {arxiv},
langid       = {english},
langidopts   = {variant=american},
annotation   = {An \texttt{article} with \texttt{eprint} and
\texttt{eprinttype} fields. Note that the arXiv reference is
transformed into a clickable link if \texttt{hyperref} support
has been enabled.  Compare \texttt{baez\slash online}, which
is the same item given as an \texttt{online} entry},
hyphenation={english},
}
@article{another,
author       = {Another,Author and One more, Author},
title        = {Title},
journaltitle = {Journal},
date         = 2004,
volume       = 12,
pages        = {423-491},
version      = 3,
eprint       = {math/0307200v3},
eprinttype   = {arxiv},
langid       = {english},
langidopts   = {variant=american},
hyphenation={english},
}
\end{filecontents*}

\usepackage[russian,greek,english]{babel}
\usepackage[natbib=true,
style=numeric,
isbn=true,
url=true,
defernumbers=false,
sorting=nyt,
firstinits=true,
backend=biber,
language=auto,
autolang=other]{biblatex}

\begin{document}
\cite{AlKarapan,CypinIstorijaRPC,baez/article}
\nocite{*}
\printbibliography
\end{document}


output will be: items in english, than in Greek and in Russian. If I edit bbl file, just moving Russian entries in begining , then Greek entries and last English entry and dont run biber just cite entries and run lualatex or xelatex everything will be fine.

Biblatex/Biber sorts bibliography, and i manually move entries for given language in bbl file. I get entries sorted for a given language and language ordering i want.

Question: can I do this kind of ordering without editing bbl file

this is what i'm trying to get

I can not use multiple bibs becouse i need continious numbering

• In this case reverse ordering will work , but this is not a case. I found this 4 yars old post tex.stackexchange.com/questions/14886/… but did not understand answer :) and now version is 3.0 Sep 19, 2015 at 13:41

Of course one can just put a \sort{\field{langid}} into the sorting procedure, but that will sort the languages lexicographically, so English will appear first, then Greek and then Russian, not exactly what you want.

But incidentally, if we sort descending lexicographically we get the order you want in the MWE.

\DeclareSortingScheme{lnyt}{
\sort{
\field{presort}
}
\sort[final]{
\field{sortkey}
}
\sort[direction=descending]{\field{langid}}
\sort{
\field{sortname}
\field{author}
\field{editor}
\field{translator}
\field{sorttitle}
\field{title}
}
\sort{
\field{sortyear}
\field{year}
}
\sort{
\field{sorttitle}
\field{title}
}
\sort{
\literal{0000}
}
}


That is of course only because the sorting order you want coincides with the reverse lexicographic order of the language names.

Use sorting=lnyt now.

A more customisable but slightly more complicated solution uses the presort field and Biber's sourcemapping feature.

\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=langid, match=\regexp{\Arussian\Z}, final]
\step[fieldset=presort, fieldvalue=1]
}
\map{
\step[fieldsource=langid, match=\regexp{\Agreek\Z}, final]
\step[fieldset=presort, fieldvalue=2]
}
\map{
\step[fieldsource=langid, match=\regexp{\Aenglish\Z}, final]
\step[fieldset=presort, fieldvalue=3]
}
}
}


Each language is simply mapped to a number that is written to the presort field. The presort field is considered before the other sort fields.

This allows for sort orders that don't follow any lexicographic order.

MWE

\documentclass[a4paper]{article}
\usepackage{fontspec}
\setmainfont[
BoldFont={DeJavu Serif Bold},
ItalicFont={DeJavu Serif Italic},
BoldItalicFont={DeJavu Serif BoldItalic}
]{DeJavu Serif}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@Book{AlKarapan,
author = {Καραπαναγοπούλου, Αλέξανδρος Κ.},
title = {Η Μεγάλη Σύνοδος της Ορθοδόξου Ανατολικής Εκκλησίας},
publisher = {auto-édition},
date = {1990/1998},
volumes = {6},
langid={greek},
}
@Inbook{CypinIstorijaRPC,
author = {Владислав Цыпин},
title = {История Русской Церкви 1917--1997},
chapter = {VIII},
publisher = {Издательство Спасо-Преображенского Валаамского монастыря},
date = {1997},
urldate = {2010-10-25},
url = {http://old.pravoslavie.by/podpod.asp?id=135&Session=10},
langid={russian},
}
@Inbook{authorrus,
author = {Другой Автор},
title = {Название},
chapter = {VIII},
publisher = {Издательство},
date = {1997},
urldate = {2010-10-25},
langid={russian},
}
@article{baez/article,
author       = {Baez, John C. and Lauda, Aaron D.},
title        = {Higher-Dimensional Algebra {V}: 2-Groups},
journaltitle = {Theory and Applications of Categories},
date         = 2004,
volume       = 12,
pages        = {423-491},
version      = 3,
eprint       = {math/0307200v3},
eprinttype   = {arxiv},
langid       = {english},
langidopts   = {variant=american},
annotation   = {An \texttt{article} with \texttt{eprint} and
\texttt{eprinttype} fields. Note that the arXiv reference is
transformed into a clickable link if \texttt{hyperref} support
has been enabled.  Compare \texttt{baez\slash online}, which
is the same item given as an \texttt{online} entry},
hyphenation={english},
}
@article{another,
author       = {Another,Author and One more, Author},
title        = {Title},
journaltitle = {Journal},
date         = 2004,
volume       = 12,
pages        = {423-491},
version      = 3,
eprint       = {math/0307200v3},
eprinttype   = {arxiv},
langid       = {english},
langidopts   = {variant=american},
hyphenation={english},
}
\end{filecontents*}

\usepackage[russian,greek,english]{babel}
\usepackage[natbib=true,
style=numeric,
isbn=true,
url=true,
defernumbers=false,
sorting=nyt,
firstinits=true,
backend=biber,
language=auto,
autolang=other]{biblatex}

\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=langid, match=\regexp{\Arussian\Z}, final]
\step[fieldset=presort, fieldvalue=1]
}
\map{
\step[fieldsource=langid, match=\regexp{\Agreek\Z}, final]
\step[fieldset=presort, fieldvalue=2]
}
\map{
\step[fieldsource=langid, match=\regexp{\Aenglish\Z}, final]
\step[fieldset=presort, fieldvalue=3]
}
}
}

\begin{document}
\cite{AlKarapan,CypinIstorijaRPC,baez/article}
\nocite{*}
\printbibliography
\end{document}


• Incidentally, the second solution is more or less what PLK suggested in the question you linked to. Sep 19, 2015 at 16:44
• "But if we sort descending alphabetically you get exactly what you want." In this case yes, but not in case i need. I actually have one more language-georgian. It is set as a main language. So I want georgian bibliography in the first place than english,russian,greek . "one can just put a \sort{\field{langid}} into the sorting procedure, but that will sort the languages alphabetically, so English will appear first, then Greek and then Russian" this is not necessary, i guess biber(??) sorts by Unicode values. So it will work. Your Solution works fine...thank you very much. It is accepted Sep 19, 2015 at 19:05
• In other words, if i use your first solution (automatic sort by langid ascending or descending sorting) do not get desired result. Second general solution using 'presort' field does the job :) . Thank you. Sep 19, 2015 at 19:17
• @LevanShoshiashvili Yes think this is what I said: The first solution can only sort the languages lexicographically or possibly reverse-lexicographically. I don't understand your comment about Unicode sorting here, that should not matter, the langid field should only contain ASCII chars. In your MWE this solution worked out, since you wanted Russian, Greek, English. But for more complex sorting that does not coincide with any lexicographic order we need the second solution. Sep 20, 2015 at 5:01

Following is a simplified version of moewe's answer.

A BibTeX source file

The following BibTeX file is saved in ~/TestBib.bib.

@book{shakespeare,
author = {William Shakespeare},
title = {Hamlet},
year = {1600},
langid = {english}
}
@book{homer,
author = {Homer},
year = {8th century BC},
langid = {greek}
}
@book{tolstoy,
author = {Leo Tolstoy},
title = {War and Peace},
year = {1869},
langid = {russian}
}


A simple LaTeX document with a bibliography

\documentclass{article}
\usepackage{biblatex}
\begin{document}
\cite{shakespeare}\cite{homer}\cite{tolstoy}
\printbibliography
\end{document}


The result of running

> cd ~
> lualatex Test
> biber Test
> lualatex Test

is

English first, the others according to the default ordering

\documentclass{article}
\usepackage{biblatex}
\DeclareSourcemap {
\maps {
\map {
\step [
fieldsource=langid,
match=english,
fieldset=presort,
fieldvalue=a
]
}
}
}
\begin{document}
\cite{shakespeare}\cite{homer}\cite{tolstoy}
\printbibliography
\end{document}


English first, Russian second

We shall now add a source map to order the bibliographic entries in such a way that the English entry is first, and the Russian entry is second.

\documentclass{article}
\usepackage{biblatex}
\DeclareSourcemap {
\maps {
\map {
\step [
fieldsource=langid,
match=english,
fieldset=presort,
fieldvalue=a
]
\step [
fieldsource=langid,
match=russian,
fieldset=presort,
fieldvalue=b
]
}
}
}
\begin{document}
\cite{shakespeare}\cite{homer}\cite{tolstoy}
\printbibliography
\end{document}


The output is:

Notes

1. There's no need to pass the datatype=bibtex option to \maps(as done in moewe's answer), since bibtex is the default value of the datatype option. See p. 199 of the biblatex manual, v. 3.18b, July 12, 2022.

2. There's no need to wrap regexps in \regexp (as done in moewe's answer) unless they contain sequences of characters that are also valid TEX commands. See p. 204 of the manual.

3. The datatype of the value of the presort special field is intepreted by biblatex as a string, not as a number. (See p. 31 of the manual.) The order that applies to its values is therefore lexicographic rather than numeric. Therefore, in my opinion, it is best to avoid assigning numbers to this field (as done in moewe's answer), to avoid non-intuitive consequences. For instance, if in my "English first, Russian second" example I'd assigned the English entry a presort value of 2 and the Russian entry a presort value of 10, the Russian entry would have ended up appearing before the English entry in the bibliography.

4. Unless explicitly specified, a bibliographic entry's presort field is automatically set to mm. (See the second paragraph of section 3.6 'Sorting Options' on p. 83 of the manual.) Therefore, if there are precisely three different values of langid you wish to order by, you don't need to explicitly specify the presort value of all three of them (as done in moewe's answer). It suffices to specify the explicit presort value of only two of them, and make sure both are lexicographically less than mm.

This is also a useful piece of information to know if you want certain entries to appear at the top of the bibliography, but you don't care about specifying the relative order of the rest of the entries, as I've done in my "English first, the others according to the default ordering" example.

Note that source mappings happen before any sorting template, including the default built-in ones, described in appendixes C1-C3 of the manual, are applied. (See the second paragraph of secgion 4.5.3 'Dynamic Modification of Data' on p. 199 of the manual.) Therefore, if you use any of the default sorting templates, say nty which is the default sorting template used if you don't specify any sorting template explicitly (see section 3.1.2.1 'Package Options/Preamble Options/General' on p. 48 of the manual), the order of the entries inside each presort group will be as expected, i.e. as proscribed by the template.

5. If arranged judiciously, the number of steps and maps can be reduced significanly, and the number of finals can be reduced or even eliminated. Compare moewe's answer to my "English first, Russian second" example.

6. Regarding the semantics of final consult this answer by moewe, since the manual is unclear and even misleading about it.