Sort data in 32-bit data table (in smaller or larger number order)
Input
Starting area of data table to be sorted
Ending area of data table to be sorted
Specifies sorting order:
0 = ascending
1 = descending
Instead of using this F instruction, we recommend using the corresponding FP7 instruction: FP_DATA_SORT
The data are sorted via bubble sort in the order specified according to the value entered at input s3_Descending. Since the number of word comparisons increases in proportion to the square of the number of words, the sorting process can take some time when there are a large number of words. When the value at inputs s1_Start=s2_End, no sorting takes place.
Although this is a 32-bit instruction, the number of steps is the same as the 16-bit instruction.
if the area specified using the index modifier exceeds the limit.
if the address of the variable at input s1_Start > s2_End
if s1_Start and s2_End belong to different data areas.
if the area specified using the index modifier exceeds the limit.
if the address of the variable at input s1_Start > s2_End
if s1_Start and s2_End belong to different data areas.
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
start: BOOL:=FALSE;
(*activates the function*)
data_array: ARRAY [0..4] OF DINT:=[4,7,-32,13,5];
(*result after a 0->1 leading
edge from start: [-32,4,5,7,13]*)
asc_order: INT:=0;
(*which way to sort:
0 means ascending order
1 means descending order*)
END_VAR
In this example, the input variable sort_order is declared. However, you can write a constant directly at the input contact of the function instead.
When the variable start is set to TRUE, the function is carried out.
Since the variable sort_order is set to 1, the specified data field in sorted in descending order.BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 9 ;
NETWORK_BODY
B(B_VARIN,,output,7,1,9,3,);
B(B_VARIN,,data field[0],7,2,9,4,);
B(B_VARIN,,data field[4],7,3,9,5,);
B(B_F,F278_DSORT,,9,0,18,6,,?DEN?D@'s1'?Ds2?Ds3?AENO);
B(B_VARIN,,sort_order,7,4,9,6,);
B(B_COMMENT,,sorting order:ø^s3 = 0:ascending~ 1:descending,10,6,25,8,);
L(1,0,1,9);
END_NETWORK_BODY
END_NET_WORK
END_BODY
IF start then
F278_DSORT( s1_Start:= data_field[0],
s2_End:= data_field[4],
s3_Descending:= sort_order);
END_IF;