What is the most efficient way to sort an Html Select's Options by value, while preserving the currently selected item?
I have jQuery but I'm not sure if it has any built-in sorting helpers. I could make a 2d array of each item's text , value , and selected properties, but I don't think that javascript's built in Array.sort() would work correctly. Extract options into a temporary array, sort, then rebuild the list: var my_options = $("#my_select option"); var selected = $("#my_select").val(); my_options.sort(function(a,b) { if (a.text > b.text) return 1; if (a.text < b.text) return -1; return 0 }) $("#my_select").empty().append( my_options ); $("#my_select").val(selected); Mozilla's sort documentation