5

L A TEX3 comes with a facility to sort list variables (sequences, token lists, or comma-lists) according to some user-defined comparison.

I try to adapt the interface3 manuel example to a token list, but it fails.

! Missing number, treated as zero.

Maybe the argument BCA is counted as one token, that's a second is missing for sorting procedure, but then how to trasform BCA in ABC ?

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\tl_new:N \l_foo_tl

\NewDocumentCommand{\Sort}{m}{%

\tl_set:Nn \l_foo_tl { #1 }
\tl_sort:Nn \l_foo_tl
{
    \int_compare:nNnTF { ##1 } > { ##2 }
    { \sort_return_swapped: }
    { \sort_return_same: }
}   

\l_foo_tl

}

\ExplSyntaxOff

\begin{document}

\Sort{BCA}

\end{document}
5
  • You're not comparing numbers
    – egreg
    Mar 23, 2018 at 21:29
  • Are you having uppercase and lowercase letters? The predefined function to convert letters to numbers in expl3 is \int_from_alph:n { a }.
    – Manuel
    Mar 24, 2018 at 0:23
  • No haven't lowercase letters in a name of a triangle and there will be it's no a problem they come after uppercase, the poit to name the triangle allways with the same ABC not BCA, but I call the macro with BCA because it is rectangle in B. Thank you for the convert to number function, what is your idea with this suggestion ?
    – Tarass
    Mar 24, 2018 at 8:30
  • @Tarass egreg finally added that to the answer below. I didn't want to add an answer just to add that idea.
    – Manuel
    Mar 24, 2018 at 10:38
  • @Manuel Yhank you I understand your point now.
    – Tarass
    Mar 24, 2018 at 14:29

1 Answer 1

6

You need to compare numbers. If your input only consists of characters, you can use

`<char>

Here's a working code:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\tl_new:N \l_tarass_sort_tl

\NewDocumentCommand{\Sort}{m}
 {
  \tl_set:Nn \l_tarass_sort_tl { #1 }
  \tl_sort:Nn \l_tarass_sort_tl
   {
    \int_compare:nNnTF { `##1 } > { `##2 }
    { \sort_return_swapped: }
    { \sort_return_same: }
   }

  \tl_use:N \l_tarass_sort_tl
 }

\ExplSyntaxOff

\begin{document}

\Sort{BCA}

\end{document}

If you want to consider equal upper and lower case versions of the same letters, use \int_from_alph:n that returns 1 from A or a, 2 from B or b, and so on, so

    \int_compare:nNnTF { \int_from_alph:n { ##1 } } > { \int_from_alph:n { ##2 } }

would sort bCA into AbC, whereas the code above would produce ACb.

10
  • Thank you. I looked for other prefix_compare, but didn't find anything for caracters. Where is ` documented and is there some other usefull trick as this one to know, and where to find them ;-) Thank you again.
    – Tarass
    Mar 23, 2018 at 21:37
  • @Tarass Backquote-character is standard TeX syntax for an alphabetical constant, denoting the character’s ASCII code. There are no interfaces for string comparison, at the moment.
    – egreg
    Mar 23, 2018 at 22:12
  • I made a function to reuse the sorting process on characters, it works but I prefer someone check and improve it (there is several type type of functions and other possible mistakes). Do I post it here as an edit or as a new question ?
    – Tarass
    Mar 23, 2018 at 22:18
  • @Tarass There is CodeReview.SE
    – egreg
    Mar 23, 2018 at 22:30
  • The tex tag says : TeX is a typesetting system, where the output is defined by command-sequences. Note that http://tex.stackexchange.com is specifically dedicated to TeX questions. and is not very active : 29 questions in 4 years.
    – Tarass
    Mar 23, 2018 at 22:35

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.