I'm trying to sort an array of Select options, but the options contain a number at the end of the string. So how can I sort not only by the beginning letter, but also by the number?
<select id="DueOn">
<option value="**SelectValue**">Enter custom value</option>
<option value="1182" >Business Day - 1</option>
<option value="1199" >Business Day - 10</option>
<option value="1801" >Business Day - 12</option>
<option value="1285" >Business Day - 15</option>
<option value="1198" >Business Day - 2</option>
<option value="1232" >Calendar Day - 4</option>
<option value="1191" >Calendar Day - 5</option>
<option value="1306" >Calendar Day - 7</option>
<option value="1782" >Calendar Day - 9</option>
<option value="1757" >Day of the Week Day - Friday</option>
<option value="1770" >Day of the Week Day - Wednesday</option>
</select>
I modified this to run and it sorted it alphabetically:
function sortSelect(selElem) {
var tmpAry = new Array();
for (var i=0;i<selElem.options.length;i++) {
tmpAry[i] = new Array();
tmpAry[i][0] = selElem.options[i].text;
tmpAry[i][1] = selElem.options[i].value;
}
tmpAry.sort();
console.log(tmpAry )
while (selElem.options.length > 0) {
selElem.options[0] = null;
}
for (var i=0;i<tmpAry.length;i++) {
var op = new Option(tmpAry[i][0], tmpAry[i][1]);
selElem.options[i] = op;
}
return;
}
sortSelect(document.querySelector("#DueOn"))
So far the only results I've gotten are Business Day - 1 Business Day - 10 Business Day - 12 Business Day - 15 Business Day - 2...