Sort data in 16-bit data table
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.
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 INT:=[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
When the variable start is set to TRUE, the function is carried out.
The constant 0 is specified at input s3, which means the sorting is carried out in an ascending order. However, you can declare a variable in the POU header and write it in the function in the body at input s3.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,8,1,10,3,);
B(B_VARIN,,data field[0],8,2,10,4,);
B(B_VARIN,,data field[4],8,3,10,5,);
B(B_F,F277_SORT,,10,0,19,6,,?DEN?D@'s1'?Ds2?Ds3?AENO);
B(B_VARIN,,0,8,4,10,6,);
B(B_COMMENT,,sorting order:ø^s3 = 0:ascending~ 1:descending,11,6,26,8,);
L(1,0,1,9);
END_NETWORK_BODY
END_NET_WORK
END_BODY
IF start then
F277_SORT( s1_Start:= data_field[0],
s2_End:= data_field[4],
s3_Descending:= 0);
END_IF;