# Text sorting and transformation

This chapter is dedicated to the text sorting and transformation, which is a subpart of the standard text processing module.

## Text sorting

Sorting is one the primary function implemented inside the text processing module. There are three sorting functions available in the module.

### Ascending and descending order sorting

The `sort-ascent` function operates with a vector object and sorts the elements in ascending order. Any kind of objects can be sorted as long as they support a comparison method. The elements are sorted in placed by using a quick sort algorithm.

# create an unsorted vector const v-i (Vector 7 5 3 4 1 8 0 9 2 6) # sort the vector in place afnix:txt:sort-ascent v-i # print the vector for (e) (v) (println e)

The `sort-descent` function is similar to the `sort-ascent` function except that the object are sorted in descending order.

### Lexical sorting

The `sort-lexical` function operates with a vector object and sorts the elements in ascending order using a lexicographic ordering relation. Objects in the vector must be literal objects or an exception is raised.

## Transliteration

Transliteration is the process of changing characters my mapping one to another one. The transliteration process operates with a character source and produces a target character with the help of a mapping table. The transliteration process is not necessarily reversible as often indicated in the literature.

### Literate object

The `Literate` object is a transliteration object that is bound by default with the identity function mapping. As usual, a predicate is associate with the object.

# load the text module interp:library "afnix-txt"

# create a transliterate object const tl (afnix:txt:Literate)

# check the object afnix:txt:literate-p tl # true

The transliteration process can also operate with an escape character in order to map double character sequence into a single one, as usually found inside programming language.

# create a transliterate object with an escape character const tl (afnix:txt:Literate '\\')

### Transliteration configuration

The `set-map` configures the transliteration mapping table while the `set-escape-map` configure the escape mapping table. The mapping is done by setting the source character and the target character. For instance, if one want to map the tabulation character to a white space, the mapping table is set as follow:

tl:set-map '\t' ' '

The escape mapping table operates the same way. It should be noted that the mapping algorithm translate first the input character, eventually yielding to an escape character and then the escape mapping takes place. Note also that the `set-escape` method can be used to set the escape character.

tl:set-map '\t' ' '

### Transliteration process

The transliteration process is done either with a string or an input stream. In the first case, the `translate` method operates with a string and returns a translated string. On the other hand, the `read` method returns a character when operating with a stream.

# set the mapping characters tl:set-map '\h' 'w' tl:set-map '\e' 'o' tl:set-map '\l' 'r' tl:set-map '\o' 'd'

# translate a string tl:translate "helo" # word