Connecting Tech Pros Worldwide Forums | Help | Site Map

Sorting options through Javascript

Newbie
 
Join Date: Dec 2007
Posts: 1
#1: Dec 8 '07
Hi,

I want to filter and sort my optionlists.

I tried to put the options in a variabel and know it is an object and actually I want it in an array, because as soon it is in an array I can sort and filter it.

Expand|Select|Wrap|Line Numbers
  1. oToSortOptions = document.forms[0].selectId.options;
  2. alert( oToSortOptions);
  3.  
The alert let me know I´v got a object, but I want the array.

Myself think it must be very simple
ScubaDo

Familiar Sight
 
Join Date: Feb 2007
Posts: 207
#2: Dec 9 '07

re: Sorting options through Javascript


Quote:

Originally Posted by Scubadreams

Hi,

I want to filter and sort my optionlists.

I tried to put the options in a variabel and know it is an object and actually I want it in an array, because as soon it is in an array I can sort and filter it.

Expand|Select|Wrap|Line Numbers
  1. oToSortOptions = document.forms[0].selectId.options;
  2. alert( oToSortOptions);
  3.  
The alert let me know I´v got a object, but I want the array.

Other than applying a sort function to the options, the only way I know is to copy the text properties into an array, sort the array, then create a new set of options using the original options to associate the sorted .text properties with the original .value properties.
Finally copy the new options into the select element.

Here are two select sort functions with the same functionality. The first performs a bubblesort, the second (recommended) uses the above algorithm.
Expand|Select|Wrap|Line Numbers
  1. function selectSort(box)
  2. {
  3.  var didSwap=true, tmp={}, v=box.options,
  4.      currSelText=(box.selectedIndex==-1?"":box[box.selectedIndex].text);
  5.  
  6.  while(didSwap)
  7.   for(var i=0, didSwap=false, len=v.length-1; i<len; i++)
  8.    if(v[i].text > v[i+1].text)
  9.    {
  10.     tmp.opt=v[i];
  11.     tmp.dis=tmp.opt.disabled;
  12.     v[i]=new Option(v[i+1].text, v[i+1].value);
  13.     v[i].disabled=v[i+1].disabled;
  14.     v[i+1]=new Option(tmp.opt.text, tmp.opt.value);
  15.     v[i+1].disabled=tmp.dis;
  16.     didSwap=true;
  17.    } 
  18.  
  19.  for(var i=0, len=v.length; i<len && v[i].text!=currSelText; i++)
  20.  ;
  21.  
  22.  if(i<len)
  23.   box.selectedIndex=i;
  24. }
  25.  
  26.  
  27. function sortSelect( box ) /*53637269707465726C61746976652E636F6D*/
  28. {
  29.  /*  Sorts the options of <select> element 'box' alphabetically 
  30.   *  by their .text property. Preserves the selected option and
  31.   *  the .value property and disabled status of each option.
  32.   */ 
  33.  
  34.  var currSelText=(box.selectedIndex==-1?"":box[box.selectedIndex].text);
  35.  
  36.  var opts=box.options, textVals=[], newOpts=[], newIndex=-1;
  37.  
  38.  for(var i=0,len=opts.length; i<len; i++)
  39.   textVals[i]=opts[i].text; 
  40.  
  41.  textVals.sort();
  42.  
  43.  for(var i=0, len=opts.length; i<len; i++)
  44.  {
  45.   for(var j=0; j<len && opts[j].text!=textVals[i]; j++)  
  46.   ;
  47.   newOpts[i]=new Option(textVals[i], opts[j].value);
  48.   newOpts[i].disabled=opts[j].disabled;
  49.  
  50.   if(currSelText==textVals[i])
  51.    newIndex=i;
  52.  }
  53.  
  54.  for(var i=0; i<len; i++)
  55.   opts[i]=newOpts[i];
  56.  
  57.  box.selectedIndex=newIndex; 
  58. }
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#3: Dec 9 '07

re: Sorting options through Javascript


turn a collection into an array:

[PHP]
function obValsl(ob) {
var r = [], i = 0, mx = ob.length;
for (var z = 0; z < mx; z++) {
r[z] = ob[z];
}
return r;
}[/PHP]

you then use a custom sort function, and apply the resultant array to the options collection variable.
Reply