| THE META-HTML LANGUAGE REFERENCE MANUAL

| [TOC] |

## Manual Page Viewer

Name: SORT

Section: Arrays

Synopsis:

`<sort ARRAYVAR [SORT-FUN] [CASELESS=TRUE] [SORTORDER=REVERSE] [NUMERIC=TRUE]>`

Description:

Sort the contents of the array ARRAYVAR.

The elements are sorted in place -- this function has no return value.

If CASELESS=TRUE is given, then the comparison of the elements of the array is done without regards to case.

If SORTORDER=REVERSE is given, then the results are returned in descending order, instead of ascending order. The default is to order the elements in ascending order.

If NUMERIC=TRUE is given, then the elements of ARRAYVAR are treated as numeric entities, whether they are or not. The default is to treat the elements as character strings, which can have unexpected results when sorting numeric quantities ("11" is less then "2" when sorting alphabetically!)

Finally, you may supply a sorting function, whose name is passed as SORT-FUN. This function will be called on each element just before comparison, and the results of that function will be used for the comparison instead of the element itself. This allows you to create a collating sort, or to sort on complex weighting features, or anything else that you can conceive of.

Examples:

Given the array:

    <set-var array[0] = 1
             array[1] = 2
             array[3] = 3
             array[4] = 4
             array[5] = 20>

then,

    <sort array>
    <foreach x array> <get-var x> </foreach>

produces:

    1  2  20  3  4

while

    <sort array numeric=true>
    <foreach x array> <get-var x> </foreach>

produces:

    1  2  3  4  20

Sorting strings:

    <set-var array[]="a\nb\nc\nd\ne\nf\nA\nB\nC\nD\nE\nF">
    <sort array sortorder=descending>
    <foreach x array> <get-var x> </foreach>

produces:

    f  e  d  c  b  a  F  E  D  C  B  A

Without regards to case:

    <sort array caseless=true>
    <foreach x array> <get-var x> </foreach>

produces:

    a  A  b  B  c  C  d  D  e  E  f  F

Finally, here is an example which sorts a list of words based upon the percentage of vowels present in each word, using a sort function which calculates that value for each string:

    <defun vowel-percentage string>
      <set-var x =
        <subst-in-string <downcase <get-var string>> "([^aeiou])" "">>
      <percent <string-length <get-var x>>
               <string-length <get-var string>>>
    </defun>

    <set-var words[]=
      <prog
        Brian
        Fox
        sorts
        elegant
        strings
        beautifully>>

    <sort words vowel-percentage numeric=true sortorder=descending>

    <foreach word words>
      <get-var word> (<vowel-percentage <get-var word>>)<br>
    </foreach>

produces:

    beautifully (45.45)
      elegant (42.92)
      Brian (40.00)
      Fox (33.33)
      sorts (20.00)
      strings (14.29)

Edit Pointer
Function Index
Variable Index
Variable Editor

The META-HTML Reference Manual V1.4 Copyright © 1995, 1996, Brian J. Fox, 1996, 1997 Universal Access Inc.
Found a bug? Send mail to bug-manual@metahtml.com