sortDropDownListByText
HTML
<body>
<form>
<select name="option1" id="id_option1">
<option value="Small" selected="selected">Small</option>
<option value="Large">Large</option>
<option value="Medium">Medium</option>
<option value="XL">XL</option>
</select>
</form>
</body>
JavaScript
function sortDropDownListByText() {
// Loop for each select element on the page.
$("select").each(function() {
// Keep track of the selected option.
var selectedValue = $(this).val();
// Sort all the options by text. I could easily sort these by val.
$(this).html($("option", $(this)).sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}));
// Select one option.
$(this).val(selectedValue);
});
};