What is the best way to insert value in an array and keep the array sorted?
for example here is an array
const arr = [1, 4, 23, 45];
I can add a new value using method push or splice, for example 16, and I'll get modified array:
[1, 4, 23, 45, 16]
But I need to keep array sorted:
[1, 4, 16, 23, 45]
What is the better way to keep array sorted? Should I sort every time when add a new value, or detect necessary index for inserting a new value?