CalcIt Commands

SORT(arr, StartPosition=1, Width)

We can sort the contents of an array using SORT command.

e.g. SORT(arr,10,5).

This example sorts the contents of array variable arr using as a sort key the part of every string starting at 10 for 5 characters. Sorting is not case sensitive. Start position and length parameters are optional. Sorting is not case sensitive.

SORT(arr, CompareFunction)

If any other sorting capability fails to satisfy your needs then use this version of SORT. It can sort an array in any imaginable way. The second parameter takes the name of a local function which is used by SORT to make the sort. This local function must have exactly two normal (IN) parameters. In the body of the function the user must compare the two parameters with < (or ls) for ascending order, or  > (or gr) for descending order, and return the result of the comparison. e.g.

set arr=[23,56,12,90,45,23,67,100,26];

FUNCTION Compare(a1,a2);
 a1<a2;
END;

Sort(arr,Compare);

PrintArr(arr);

The two parameters can be processed in any way before the comparison

SORT(ArrExpr)

Returns a sorted version of the contents of ArrExpr. The sort is alphabetical with no case sensitivity. It is used in array expressions.

SORT(NufOfElements, SortingProc)

This version of sort can sort anything and not only dynamic arrays. The user provides a local function which handles element swapping and comparison for the command.

  1. NumOfElements. Is an expression returning a numeric value representing the number of elements to sort.
  2. SortingProc. Is a local function the user provides so the sort command will be able to swap and compare items and perform correctly the sorting operation. This local function must have a specific interface:

    SortingProc(ForSwap,ItemNo1,ItemNo2)

    When it is called for swapping then ForSwap parameter is true (1). The user executes a swap between elements ItemNo1 and ItemNo2 of a structure he/she knows what it is. In this case the returned value is not used. If ForSwap is false (0) then the function is called to compare the elements ItemNo1 and ItemNo2. The user has to use the < (for numeric) or ls (for alphanumeric values) to perform a descending sorting. If he/she wants an ascending sorting then he/she can use > (for numeric) or gr (for alphanumeric) operators instead.

    NOTE: ItemNo1, ItemNo2 parameters return one based values, that is they denote the first element at value of 1.

Example: As an example will sort a dynamic array using this generic version of sort:

Clear;

set d=[10,34,23,12,67,45,87,55,33,15,7];

function SortProc(swap,i1,i2);
 if(swap);
  v:=d(i1);
  d(i1):=d(i2);
  d(i2):=v;
 else
  d(i1)<d(i2);
 end;
end;

Sort(size(d),SortProc);


PrintArr(d);

SORTN(arr)

Numerically sorts the elements of array arr.

SORTN(ArrExpr)

Returns the numerically sorted version of ArrExpr.

It is used in array expressions.

Go Back