# sort()

Sort a list of numeric or alphabetic values.

## Syntax

``sort(list,[mode],[position])``

## Parameter

Parameter

Definition

`list`

PSL list whose elements that are to be sorted

`mode`

optional character string specifying the sort order*Valid Values*
n ascending numeric order
nr descending numeric order
"" ascending alphabetic order
r descending alphabetic order
Default
`""` (ascending alphabetic)

`position`

optional integer that specifies the character position within each element of list where sorting is to begin The first character of each list element is character 1. If the length of every element within list is less thanposition, the effect is the same as if all the elements of list were NULL elements. The position does not truncate elements; it only ignores the first ( position - 1) characters for purposes of comparison.Default
1

## Description

The sort() function returns a sorted version of list that is ordered according to mode

The sort() function does not merge duplicate entries in list. The returned list has the same number of members as list. The order in which duplicates are returned is not defined because it is not defined for the C library qsort() function. This fact is relevant for the following cases:

• numeric sorting of strings with identical numeric prefix values but different nonnumeric suffixes
• any sorting mode in which position is larger than more than one element within list (the sort() function regards all such elements as duplicate NULL elements)

If list is the NULL list, the sort function returns the NULL list. For a nonempty list, the sort() function always returns a well-defined list with the last line properly terminated by a new-line character.

The list does not need to be terminated by a new-line character. Numeric sorting is based on floating point values; nonnumeric list entries are converted according to the system's standard C library function atof().

## Example

The following example shows the different sorting orders available in the sort() function:

``````function print_list(label,array) {
printf("%s",label);
foreach member (array) {
printf("%2d ",trim(member,"\n"));
}
printf("\n");
}
function main() {
srandom(time());
i = 0;
while (i <= 15) {
list = [list,random(100)];
i++;
}
print_list("unsorted list : ",list);
print_list("sorted asc. numeric : ",sort(list,"n"));
print_list("sorted desc. numeric : ",sort(list,"nr"));
print_list("sorted asc. alpha : ",sort(list,""));
print_list("sorted desc. alpha : ",sort(list,"r"));
}``````

The example produces the following output:

``````unsorted list : 90 6 68 84 59 52 4 67 74 15 48 46 34 88 76 27
sorted asc. numeric : 4 6 15 27 34 46 48 52 59 67 68 74 76 84 88 90
sorted desc. numeric : 90 88 84 76 74 68 67 59 52 48 46 34 27 15 6 4
sorted asc. alpha : 15 27 34 4 46 48 52 59 6 67 68 74 76 84 88 90
sorted desc. alpha : 90 88 84 76 74 68 67 6 59 52 48 46 4 34 27 15``````

Submitting... Thank you