Let's say I've created a large numbered list of items with some cyrillic characters, but now I want to reorder them alphabetically. By the way the numbers at each item should also be reordered. I have read the Alphabetically display the items in itemize article, but the method here was shown to fail in my case (perhaps I did smth wrong). Isn't it just easier to put the whole code into Excel and be happy with its sorting tool?

Here is what I have:

1) БВГ

2) АБВ

3) ДЕЖ

4) ХАУ

There is what I need:

1) АБВ

2) БВГ

3) ДЕЖ

4) ХАУ

The code to work with:



        \item БВГ
        \item АБВ
        \item ДЕЖ
        \item ХАУ


xindy does a nice job of sorting and there's a lot of questions in TeX.SX about it. However, if you accept a LuaTeX-based solution without external software, there's a new package lua-uca which supports the basics of Unicode Collation Algorithm. You fortunate! Russian is among the languages predefined in the package, so let's use it:

%!TEX program = lualatex
%Change the font if necessary
\setmainfont{Noto Sans}
%For using \luadirect and luacode* environment
%Copied from the documentation almost literally
kpse.set_program_name "luatex"

ducet = require "lua-uca.lua-uca-ducet"
collator = require "lua-uca.lua-uca-collator"
languages = require "lua-uca.lua-uca-languages"

collator_obj = collator.new(ducet)
collator_obj = languages.ru(collator_obj)

function russian_sort(mylist)
    table.sort(mylist, function(a,b)return collator_obj:compare_strings(a,b)end)
    return mylist

%Printing as list
function table_to_tex(mylist)
    for _, v in ipairs(mylist) do
        tex.print([[\item]].." "..v)

ourlist = ourlist or {}

%Passing TeX content to Lua
\newcommand{\addtoourlist}[1]{\luadirect{table.insert(ourlist, \luastringN{#1})}}
%Print an ordered list
\newcommand\printtoourlist{\directlua{russian_sort(ourlist) table_to_tex(ourlist)}} 

enter image description here

| improve this answer | |
  • 1
    Thanks! Wouldn't say I'm in good relationship with Lua, but I'll take a look. Btw never heard of xindy, got to try. – Rubisko Jun 8 at 5:44
  • Oh, I've forgotten to say you have to use TeX Live 2020 (the package is fresh new). In case you use MiKTeX, let it install on-the-fly. :) – user193767 Jun 8 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.