F277_SORT

Sort data in 16-bit data table

The function sorts values (with +/- sign) in a data table in ascending or descending order.

Parameters

Input

s1_Start (INT)

Starting area of data table to be sorted

s2_End (INT)

Ending area of data table to be sorted

s3_Descending (INT)

Specifies sorting order:

  • 0 = ascending

  • 1 = descending

Remarks

  • 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.

Error flags

sys_bIsOperationErrorHold (turns to TRUE and remains TRUE)
  • 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.

sys_bIsOperationErrorNonHold (turns to TRUE for one scan)
  • 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.

Example

POU header

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

POU body

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.

LD body

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

ST body

IF start then
    F277_SORT( s1_Start:= data_field[0],
         s2_End:= data_field[4],
         s3_Descending:= 0);
END_IF;

IL body

Modified on: 2022-08-23Feedback on this pagePanasonic hotline