The key change to the demo is this code:
tabs.find( ".ui-tabs-nav li:nth-child(" + index + ")" ) .before( li );
This is different from original approach of simply appending the item. Here, the
indexrepresents the 1-based position of the new tab. We then find that element and insert the new tab before it. The order in which the new tab content div is inserted doesn't matter it's looked up by ID.