views:

3170

answers:

6

Hi, is there a quick way to sort the items of a select element? Or I have to resort to writing javascript?

Please any ideas.

<select size="4" name="lstALL" multiple="multiple" id="lstALL" tabindex="12" style="font-size:XX-Small;height:95%;width:100%;">
<option value="0"> XXX</option>
<option value="1203">ABC</option>
<option value="1013">MMM</option>
</select>
A: 

You'll have to use JavaScript. Do you want to sort by each options value or display name?

Matty
Oops, new to the site. Didn't realize that we could post comments instead of commenting using an answer.
Matty
+1  A: 

Yes DOK has the right answer ... either pre-sort the results before you write the HTML (assuming it's dynamic and you are responsible for the output), or you write javascript.

The Javascript Sort method will be your friend here. Simply pull the values out of the select list, then sort it, and put them back :-)

Joel Martinez
+2  A: 

From the W3C FAQ:

Although many programming languages have devices like drop-down boxes that have the capability of sorting a list of items before displaying them as part of their functionality, the HTML <select> function has no such capabilities. It lists the <options> in the order received.

You'd have to sort them by hand for a static HTML document, or resort to Javascript or some other programmatic sort for a dynamic document.

Bill the Lizard
+4  A: 

This will do the trick. Just pass it your select element a la: document.getElementById('lstALL') when you need your list sorted.

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();
  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;
 }
Matty
This sorts the elemnts by the text, if you want to sort by value you should set value as index 0 !-)
roenving
That's true. I think the author mentioned wanting to sort by display name. Could be rewritten to take an additional parameter indicating whether to sort on display or value.I'm also wondering if there is a cleaner way of copying the options structure to an array.
Matty
Nice job, runs really quick even on ~ 1000 options.
NateReid
+1  A: 

I had the same problem. Here's the jQuery solution I came up with:

  var options = jQuery.makeArray(optionElements).
                       sort(function(a,b) {
                         return (a.innerHTML > b.innerHTML) ? 1 : -1;
                       });
  selectElement.html(options);
Marco Lazzeri
Works, and since is less code then the other solutions - it wins. Hoever, the currently selected item gets borked. You get a "+" if you fix this also for multi select !
elcuco
A: 

Not quite as pretty as the JQuery example by Marco but with prototype (i may be missing a more elegant solution) it would be:

function sort_select(select) {
  var options = $A(select.options).sortBy(function(o) { return o.innerHTML });
  select.innerHTML = "";
  options.each(function(o) { select.insert(o); } );
}

And then just pass it a select element:

sort_select( $('category-select') );
Geoff