The simple answer is - just make sure you move them in the right order.
An option (pardon the pun!) is to add a new attribute to each option element in the select element.
You may wish to add an attribute called (say) 'origIndex' - this will be used for nothing other than maintaining the same order when an item is moved from one list to another.
When it comes time to move an item, you simply get the srcItem's origIndex value. You then go through the options in the destList and when the item being examined has a origIndex that is higher than the item being moved you just use the insertBefore method of the 2nd Select element.
Here's some code that will do it. Tested in chrome.
<!DOCTYPE html>
<html>
<title>Dialog Page</title>
<head>
<script>
function byId(e){return document.getElementById(e);}
function makeList(strListId, numOptions)
{
var sel, opt, i, x;
sel = document.createElement('select');
sel.setAttribute('id', strListId);
for (i=0; i<numOptions; i++)
{
x = Math.floor(Math.random() * 100);
opt = document.createElement('option');
opt.appendChild(document.createTextNode(x) );
opt.setAttribute('name', x );
opt.setAttribute('origIndex', i);
sel.appendChild(opt);
}
return sel;
}
function moveSelected(idListSrc, idListDest)
{
src = byId(idListSrc);
dest = byId(idListDest);
selIndex = src.selectedIndex;
if (selIndex != -1)
{
selectedElement = src.options[selIndex];
sortedInsert(dest, selectedElement);
}
else
{
alert('No option selected');
}
}
function sortedInsert(destList, elementToAdd)
{
var numOptions, i, addIndex, curIndex;
numOptions = destList.options.length;
if (numOptions > 0)
{
addIndex = parseInt(elementToAdd.getAttribute('origindex'));
for (i=0; i<numOptions; i++)
{
curIndex = parseInt(destList.options[i].getAttribute('origindex'));
if (addIndex < curIndex)
{
destList.insertBefore(elementToAdd, destList.options[i]);
return;
}
}
destList.appendChild(elementToAdd);
}
else
destList.appendChild(elementToAdd);
}
function myInit()
{
parent = byId('holder1');
parent.appendChild(makeList('list1', 10));
btn = document.createElement('button');
btn.setAttribute('id', 'moveBtn');
btn.appendChild(document.createTextNode('Move -->'));
btn.onclick = function() { moveSelected('list1', 'list2'); }
parent.appendChild(btn);
parent.appendChild(makeList('list2', 0));
}
</script>
</head>
<body onload="myInit();">
<div id='holder1'></div>
</body>
</html>