If you really need to use the setOrder () method, I will work with order numbers 10, 20, 30, 40, ... Thus, it will be easy to add elements between already installed elements.
In addition, to avoid using order numbers twice, you can use an array in which you store all numbers from 1 to X. Whenever you set an order number, you set it using the getOrderNumberFromArray () method, which returns the next higher number or lower order, still available in the array, and disables that element of the array.
Alternatively, or maybe even better, you can do getOrder () for the element you want to have before the new element, then increment that serial number by X, and then scroll through existing form elements and verify that the order number is not working Not yet.
Or you can simply use getOrder () on the element that you want to show before and after the new element, and make sure that you are not using the same sequence numbers for the new element.
source share