(1)西门子PLC(用的FC,带变长数组功能)
IF #enable = 1 THEN //使能
#tempup := UPPER_BOUND(ARR := #array, DIM := 1);//读取数组上限
#templow := LOWER_BOUND(ARR := #array, DIM := 1);//读取数组下限
FOR #i := #templow TO #tempup BY 1 DO //外循环
FOR #j := #templow TO (#tempup - #i - 1) BY 1 DO;//内循环
//升序
IF #array[#j] > #array[#j + 1] THEN //前后字比较,条件移位
#temp := #array[#j + 1];
#array[#j + 1] := #array[#j];
#array[#j] := #temp;
END_IF;
END_FOR;
END_FOR;
END_IF;
(2)三菱PLC(只能用FB创建)
IF i_enable = 1 THEN //使能
FOR ii := 0 TO i_number DO; //外循环
FOR jj := 0 TO (i_number -ii)DO;//内循环
//升序
IF i_array[jj] >i_array[jj+1]THEN //前后字比较,条件移位
temp:= i_array[jj+1];
i_array[jj+1]:=i_array[jj];
i_array[jj]:=temp
;
END_IF;
END_FOR;
END_FOR;
END_IF;