Function: sort_text

Function: sort_text

Usage

 sort_text(order: order specification, some_text)

Sort a string or filter it by keeping specific characters.

When the order parameter is not given, the input is sorted in alphabetical order.

Otherwise the order parameter determines what characters to show in the output. The order is a string consisting of multiple parts, each part selects a specific thing from the input. Parts

Part sort("zzyyxxy") Description
"x" "xx" A normal character, selects all matching characters from the input
" " "" Whitespace is ignored.
"\\<" "" Characters can be escaped with a backslash, normally the character "<" has special behaviour. Note that you also need to escape the backslash for the string.
"<wxy>" "xy" Selects the characters from the input, but only once. "<xy>" is the same as "<x><y>".
"[wxy]" "yyyxx" Selects the given characters, but keep them in the same order as in the input.
"(xwz)" "zzxx" The same as "cycle(xyz)".
"ordered(wxy)" "xxyyy" The same as just "wxy".
"once(wxy)" "xy" The same as "<wxy>".
"mixed(wxy)" "yyyxx" The same as "[wxy]".
"compound(yx)" "yx" Selects the literal substring "xy" only where it matches exactly. Selects it as many times as it appears
"any()" "zzyyxxy" Selects the remaining part of the input.
"cycle(xwz)" "zzxx" Selects the given characters, and put them in the order that minimizes the distance. around a 'circle'. Imagine w x and y in a circle, then "zx" is shorter then "x.z" because the latter passes the "w" space. This sorting option is used to sort colored mana symbols in Magic.
"reverse_order(x y)" "yyyxx" Inside the parentheses can be multiple parts (that can again include part-functions). "reverse_order(x y)" is not the same as just "y x", because in the first case x is evaluated first. For example "x compound(xy)" doesn't work, because the xs are all matched by the time the compound part is evaluated. So instead you can use "reverse_order(compound(xy) x)".
"pattern(.z. xyz)" "yzz" Selects all things that match the pattern, where "." is a wildcard. The things matching the wildcards are then sorted using the given pattern (separated by a space), and subsituted back in. So in "zzyyxxy" the pattern matches "zzyxxy" with wildcards "zy" these sort as "yz" and in the pattern this becomes "yzz".
"in_place(yz)" "yyyzxxz" Sort everything with the given pattern, and insert the remaining characters in their orignal places.
For example, in "zzyyxxy" we match y and z, leaving "....xx.". The result of the pattern is "yyyzz", subsituting that back in gives "yyyzxxz".

The parts are read from left to right, each part that matches something removes it from the input, so for example

 sort_text(order: "xx")

behaves the same as

 sort_text(order: "x")

because the first "x" already selects all xs from the input.

Parameters

Parameter Type Description
input string Text to sort
order string (optional) Order specification

Examples

 sort_text("banana")                                      ==  "aaabnn"
 sort_text(order: "na",                        "banana")  ==  "nnaaa"
 sort_text(order: "n a",                       "banana")  ==  "nnaaa"
 sort_text(order: "<na>",                      "banana")  ==  "na"
 sort_text(order: "once(na)",                  "banana")  ==  "na"
 sort_text(order: "[na]",                      "banana")  ==  "anana"
 sort_text(order: "mixed(na)",                 "banana")  ==  "anana"
 sort_text(order: "compound(na)",              "banana")  ==  "nana"
 sort_text(order: "reverse_order(na)",         "banana")  ==  "aaann"
 sort_text(order: "a n <a>",                   "banana")  ==  "aaann"
 sort_text(order: "reverse_order(<a> n a)",    "banana")  ==  "aanna"
 sort_text(order: "pattern(./. cycle(wubrg))", "wgw/g")   ==  "g/w"
 sort_text(order: "[1234567890]cycle(wubrg)",  "21wg")    ==  "21gw"

 f := sort_text@(order: "[1234567890]cycle(wubrg)")
 f("21wg")  ==  "21gw"