insert elements in sorted order