SRT — Sort
SRT in rungs.dev sorts a run of array elements into ascending order, in place. Use it to find a median, rank readings, or order a batch before working through it.
SRT
Array
array
Dimension
dimension
Control
control
Length
length
Position
position
Sorts a run of array elements into ascending order, in place.
What it is for
Sorting is how you find a median — sort the readings and take the middle one. A median throws away a single wild sample in a way an average cannot, which makes it the better choice for a sensor that occasionally spikes.
It is also how you rank things: sort the batch, then work through it smallest first.
"In place" means the array itself is reordered. There is no separate destination, and the original order is gone.
Operands
| Name | Type | Notes |
|---|---|---|
array | DINT, INT, SINT, REAL | Where the run starts — Samples or Samples[2] |
dimension | the number 0 | Which dimension to vary |
control | CONTROL | The instruction's state |
length | number | How many elements to sort |
position | number | Not used. Leave it 0 |
Elements outside [start, start + length) are never moved.
How It Works
SRT is a one-shot: it sorts on the scan the rung goes true and not again while it stays
true, even if you scramble the array in between.
What arms it is .DN, not .EN — which matters if you ever write the bits yourself:
| You write, with the rung still true | What happens |
|---|---|
.EN := 0 | Nothing. .EN stays 0 |
.DN := 0 | Sorts again, at once |
That is the opposite of the FIFO instructions, where the enable bit is the whole latch.
Afterwards .POS sits on the last element sorted, .LEN - 1. Unlike AVE,
a false rung keeps .POS — it survives a restart too.
A run past the end of the array stops the program
Where AVE and STD stop quietly with .ER, SRT raises a major fault. Studio checks the
number you type against the declared size; a .LEN written from logic is checked while the
program runs. A negative .LEN or .POS is a major fault as well.
Example — Median of Five
Sort five readings and take the middle one.
0
Sample
SRT
Array
Readings
Dimension
0
Control
SortCtl
Length
5
Position
0
1
Sample
MOVE
Source
Readings[2]
Dest
Median
Both rungs run on the same scan, and rungs run top to bottom, so the MOVE reads the array
after the sort.
Structured Text
SRT has a Structured Text form, and it is the short one — three arguments, not five:
SRT(Readings, 0, SortCtl);The length and position come from SortCtl.LEN and SortCtl.POS, not from arguments. Set
.LEN on the tag or with a MOVE before the sort runs.
Common Mistakes
- Expecting the original order back. It is gone — copy the array first with
COPif you need it. - Passing five arguments to the Structured Text form. It takes three.
- Sorting more elements than the array holds. That stops the program.
- Clearing
.ENto make it sort again. Clear.DN.
Related
- Array instructions overview
- AVE and STD — average and spread over the same run
- COP and CPS — keep a copy of the original order
AVE and STD — Average and Spread
AVE and STD in rungs.dev work out the average and the standard deviation of a run of array elements. Use them to smooth a noisy reading or to tell steady from unstable.
FAL and FSC — Loop Over an Array
FAL and FSC in rungs.dev run one expression once per array element. FAL stores each answer; FSC stops at the first element that passes. Use them to scale a batch or find a value.