# Order table by row alphabeticaly

I have a table like this:

\documentclass[10pt,spanish]{article}
\usepackage[T1]{fontenc}
\usepackage{selinput}
\usepackage{multirow}
\usepackage{tabularx}
\SelectInputMappings{%
aacute={á},
ntilde={ñ},
Euro={€}
}
\usepackage{babel}
\usepackage [left=1.5cm, right=1.5cm, top=1.5cm, bottom=1.5cm]{geometry}
\usepackage{longtable}

\begin{document}

\begin{longtable}{| p{.15\textwidth} | p{.85\textwidth} |}
\hline
Spanish     & English                                       \\ \hline
ser         & to be (essential/permanent quality)           \\ \hline
haber       & to have (to do something, auxiliary verb)     \\ \hline
estar       & to be (health, location, state)               \\ \hline
tener       & to have                                       \\ \hline
hacer       & to do, make                                   \\ \hline
poder       & to be able, can                               \\ \hline
decir       & to say, tell                                  \\ \hline
ir          & to go                                         \\ \hline
ver         & to see                                        \\ \hline
dar         & to give                                       \\ \hline
saber       & to know (information)                         \\ \hline
querer      & to want, love                                 \\ \hline
llegar      & to arrive, come, reach                        \\ \hline
pasar       & to pass, to spend (time), to happen           \\ \hline
deber       & to owe, must, should, ought to                \\ \hline
poner       & to put, place, set                            \\ \hline
parecer     & to seem, appear                               \\ \hline
quedar      & to stay, remain                               \\ \hline
creer       & to believe                                    \\ \hline
hablar      & to speak                                      \\ \hline
llevar      & to carry, bring                               \\ \hline
dejar       & to leave, abandon, to let, allow              \\ \hline
seguir      & to follow, continue                           \\ \hline
encontrar   & to find, encounter                            \\ \hline
llamar      & to call, to name                              \\ \hline
\end{longtable}

\end{document}


I am alway add in this table new words. I would like to order automatically rows by the value of the Spanishcolumn on alphabetical order (A-Z).

I really like how it was made here. I tried to do the same for my table but I failed.

• I would select the lines and then use the "tools->sort lines" option of my editor (winedt). Faster than sorting with tex. May 31, 2017 at 15:13
• I am using online editor (overleaf) and there isn't this option. Also I'd like be able to do it with tex. Anyway thank you for your comment.
– Alex
May 31, 2017 at 15:26
• TeX isn't very good at sorting; that's why BibTeX and biber were created: to sort bibliographic entries alphabetically. (Well, LuaLaTeX offers access to Lua's table functions, including table.sort. However, using LuaLaTeX would seem like overkill for what you say you want to achieve.) You may want to look into (a) placing the tabular material -- without the \hline stuff -- in a csv file, (b) using an external program to sort the material on the first column, and (c) employing something like the csvsimple package to place the tabular material inside a longtable environment.
– Mico
May 31, 2017 at 17:25
• You may also wish to look into the capabilities of the datatool package. It can perform sorting, which the csvsimple package does not. The downside is that learning how to use datatool efficiently will take a bit more effort than does loading csvsimple.
– Mico
May 31, 2017 at 17:51
• Thanks for your comments, Mico. It seems very difficult for me now. I am really new at Latex. And all I wanted is just sort my table in online editor (overleaf). As I wrote before I like the solution from the question №264418. It works great, but I just couldn't reproduce it for my table. Hovewer, I am looking forward that some day I will be able to understand things which you described :)
– Alex
May 31, 2017 at 18:03

A proof of concept, using an idea from https://tex.stackexchange.com/a/356777/4427

\documentclass{article}
\usepackage{geometry}
\usepackage{longtable}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\spanishverb}{mm}
{
\seq_gput_right:Nn \g_alex_spanishverbs_seq { \__alex_spanishverbs_entry:nn {#1}{#2} }
}

\seq_new:N \g_alex_spanishverbs_seq
\tl_new:N  \g__alex_spanishverbs_tablebody_tl

\NewDocumentCommand{\sortverbs}{}
{
\cs_gset_eq:NN \__alex_spanishverbs_entry:nn \use_i:nn
\seq_gsort:Nn \g_alex_spanishverbs_seq
{
\alex_spanishverbs_if_before:ffTF { ##1 } { ##2 }
{ \sort_return_same: }
{ \sort_return_swapped: }
}
\cs_gset_eq:NN \__alex_spanishverbs_entry:nn \alex_spanishverbs_print:nn
\tl_gset:Nx \g__alex_spanishverbs_tablebody_tl
{
\seq_use:Nn \g_alex_spanishverbs_seq { \\ \hline }
}
}
\NewExpandableDocumentCommand{\printverbs}{}
{
\tl_use:N \g__alex_spanishverbs_tablebody_tl
}

\prg_new_conditional:Nnn \alex_spanishverbs_if_before:nn { p,T,F,TF }
{% I hope the LaTeX3 police won't catch me
\int_compare:nTF { \pdftex_strcmp:D { #1 } { #2 } < 0 }
{
\prg_return_true:
}
{
\prg_return_false:
}
}
\cs_generate_variant:Nn \alex_spanishverbs_if_before:nnTF { ff }

\cs_new:Nn \alex_spanishverbs_print:nn { #1 & #2 }
\ExplSyntaxOff

\begin{document}

\spanishverb{ser}{to be (essential/permanent quality)}
\spanishverb{haber}{to have (to do something, auxiliary verb)}
\spanishverb{estar}{to be (health, location, state)}
\spanishverb{tener}{to have}
\spanishverb{hacer}{to do, make}
\spanishverb{poder}{to be able, can}
\spanishverb{decir}{to say, tell}
\spanishverb{ir}{to go}
\spanishverb{ver}{to see}
\spanishverb{dar}{to give}
\spanishverb{saber}{to know (information)}
\spanishverb{querer}{to want, love}
\spanishverb{llegar}{to arrive, come, reach}
\spanishverb{pasar}{to pass, to spend (time), to happen}
\spanishverb{deber}{to owe, must, should, ought to}
\spanishverb{poner}{to put, place, set}
\spanishverb{parecer}{to seem, appear}
\spanishverb{quedar}{to stay, remain}
\spanishverb{creer}{to believe}
\spanishverb{hablar}{to speak}
\spanishverb{llevar}{to carry, bring}
\spanishverb{dejar}{to leave, abandon, to let, allow}
\spanishverb{seguir}{to follow, continue}
\spanishverb{encontrar}{to find, encounter}
\spanishverb{llamar}{to call, to name}
\sortverbs

\begin{longtable}{
|
p{\dimexpr.15\textwidth-2\tabcolsep-1.5\arrayrulewidth}
|
p{\dimexpr.85\textwidth-2\tabcolsep-1.5\arrayrulewidth}
|
}
\hline
Spanish & English \\ \hline
\printverbs \\ \hline
\end{longtable}

\end{document}


• You could replace \pdftex_strcmp:D  by \__str_if_eq_x:nn, then it would work with luatex too. But there seem to be no public function to test if the result is positive or negative. Jun 1, 2017 at 7:18
• @UlrikeFischer Using either is the same: both are “forbidden”. I believe that one day there will be an alphabetical sorter in expl3, but you can understand what the problems are: different collating orders, for instance, and accented characters. Probably this particular problem should be delegated to Python or similar scripting language. Jun 1, 2017 at 7:27
• Yes I know that both are forbidden. But as the second works with all engines I think as a forbidden fruit it is better. And I understand the sorting problems, that's why I suggested in my comment directly to use an external tool. Jun 1, 2017 at 7:51
• @egreg Thanks a lot for your answer. It seems great! Unfortunadly, it doesn't work for me. I am using online editor (overleaf). And when I try to reproduce your code there, appears an error: Undefined control sequence. l.30 \NewExpandableDocumentCommand {\printverbs}{}  on the line №30: \NewExpandableDocumentCommand{\printverbs}{}. Could you help me please with it? What am I doing wrong? What should I do?
– Alex
Jun 2, 2017 at 13:29
• @Alex Unfortunately, Overleaf lags behind in updates Jun 2, 2017 at 16:09

R/Sweave/knitr approach:

1) The table content is an easy to make SPUK.csv file, with this format:

Spanish    ; English
ser        ; to be (essential/permanent quality)
haber      ; to have (to do something, auxiliary verb)
estar      ; to be (health, location, state)
...


2) Then is fairly simple in R read it as a data frame, sort it, and print the data frame as a LaTeX table with xtable library.

For a long table with headers on every page, you will need play with some options (not so easy, I admit) but here a R code that does this:

library(xtable)
options(xtable.booktabs = T)
add.to.row <- list(pos = list(0), command =NULL )
"\\bottomrule\n",
"{\\footnotesize Continued on next page}\n",
"\\endfoot\n",
"\\endlastfoot\n")
add.to.row$command <- command print(xtable(SPUK[order(SPUK$Spanish),] ,
caption = "Spanish verbs"),
caption.placement = "top",
tabular.environment = "longtable",
include.colnames = T,
floating = F, include.rownames = F)


If the result is 100% satisfactory, and you do not need make any any modification, using knitr you do not need to enter in R each time, then copy & paste the result in the LaTeX document. It will be enough insert the above code in the LateX code only one time:

\documentclass{article}
\usepackage{longtable,booktabs,lipsum}
\begin{document}
\lipsum[1-3]
<<xxx, results="asis", echo=F>>=
library(xtable)
options(xtable.booktabs = T)
add.to.row <- list(pos = list(0), command =NULL )
"\\bottomrule\n",
"{\\footnotesize Continued on next page}\n",
"\\endfoot\n",
"\\endlastfoot\n")
add.to.row$command <- command print(xtable(SPUK[order(SPUK$Spanish),] ,
caption = "Spanish verbs"),
caption.placement = "top",

If you do not know how compile this with knitr, save it as filename.Rnw, open it with Rstudio and push the Compile PDF buttom. Result: