(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;