SRT — Sort

SRT is the Structured Text instruction in rungs.dev that sorts a run of array elements into ascending order in place, using the length and position on its CONTROL tag.

Sorts a run of array elements into ascending order, in place. Modeled on the SRT instruction in Logix 5000® controllers.

SRT(Readings, 0, SortCtl);

After this statement, the first SortCtl.LEN elements of Readings are in ascending order.

Operands

NameTypeNotes
arrayDINT, INT, SINT, REALReadings or Readings[0]
dimensionthe literal 0Which dimension to vary
controlCONTROLHolds the length and the status bits

Three arguments, not five

The Ladder box has Length and Position rows; the Structured Text form does not. Both come from the control tag's .LEN and .POS members instead. Set .LEN before the sort runs — on the tag's default, or with an assignment:

SortCtl.LEN := 5;
SRT(Readings, 0, SortCtl);

dimension is always 0 — Studio supports single-dimension arrays only.

After it runs

.POS holds the index of the last element sorted, .LEN - 1. .EN and .DN turn on.

A run past the end of the array, or a negative .LEN or .POS, is a major fault: the scan stops and nothing is sorted. Use SIZE to keep .LEN honest:

SIZE(Readings, 0, SortCtl.LEN);
SRT(Readings, 0, SortCtl);

On this page