I have trouble reordering the options within a select box by value, from lowest to highest programatically. Whatever I have tried continues to order the list alphabetically, and I cannot find a way to control it based by the value.
The select field in question:
<select class="jet-select__control" name="koncert-dag">
<option value="" data-label="VÆLG DAG" data-counter-prefix="(" data-counter-suffix=")">VÆLG DAG</option>
<option value="42" data-label="Fredag" data-counter-prefix="(" data-counter-suffix=")">Fredag</option>
<option value="43" data-label="Lørdag" data-counter-prefix="(" data-counter-suffix=")">Lørdag</option>
<option value="44" data-label="Søndag" data-counter-prefix="(" data-counter-suffix=")">Søndag</option>
<option value="41" data-label="Torsdag" data-counter-prefix="(" data-counter-suffix=")">Torsdag</option>
</select>
My boss ordered this done in a brick builder (Elementor / Wordpress) which prohibits me from just arranging it like I usually would (annoying). So I am trying to force it into the order of 41 - 44 with JavaScript. My best bet is this line of code, which only flips it upside down so 41 is first, followed by 44, 43, 42.
<script>
const setId = document.querySelector('.jet-select__control');
setId.setAttribute('id', 'reorder-list');
window.onload = function sortOptions() {
var options = document.getElementById('reorder-list').options;
var optionsArray = [];
for (var i = 0; i < options.length; i++) {
optionsArray.push(options[i]);
}
optionsArray = optionsArray.sort(function (a, b) {
return a.HTMLSelectElement.options.charCodeAt(0) - b.HTMLSelectElement.options.charCodeAt(0);
});
for (var i = 0; i <= options.length; i++) {
options[i] = optionsArray[i];
}
options[0].selected = true;
}
sortOptions();
</script>
Is there a way to make it use the values instead? Or just through JavaScript simply force it to do a certain order that I define?