Solved

Sorting a drop down list box

Posted on 2000-02-28
3
216 Views
Last Modified: 2010-04-09
How can I sort a combo box after adding items to it dynamically?
0
Comment
Question by:wariar
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 50 total points
ID: 2565591
<HTML>
<HEAD>
<SCRIPT language='javascript'>
function optswap( cboListBox, i, j )
{
   if ( i == j )
   {
      return (false);
   }

   with ( cboListBox )
   {
      var opttxt = options[i].text
      var optval = options[i].value
      var optsel = options[i].selected

      options[i].text      = options[j].text;
      options[i].value   = options[j].value;
      options[i].selected   = options[j].selected;

      options[j].text      = opttxt;
      options[j].value   = optval;
      options[j].selected   = optsel;
   }

   return (true);
}


function SortListBox( cboListBox, start, end )
{
   // quick-sort algorithm for listbox widgets
   if ( start+1 < end )
   {
      var prevlow = start;

      for ( var i=start+1; i<end; ++i )
      {
         if ( cboListBox.options[i].text.toUpperCase() < cboListBox.options[start].text.toUpperCase() )
         {
            optswap( cboListBox, i, ++prevlow );
         }
      }

      optswap( cboListBox, start, prevlow );

      SortListBox( cboListBox, start, prevlow );
      SortListBox( cboListBox, prevlow+1, end );
   }
}
</script>
</head>
<BODY>
<FORM name='myform'>
<SELECT name='myselect' size='6'>
 <OPTION>Frank</option>
 <OPTION>Ralph</option>
 <OPTION>Hank</option>
 <OPTION>Fred</option>
 <OPTION>Abe</option>
 <OPTION>Gabe</option>
 <OPTION>John</option>
 <OPTION>Jim</option>
 <OPTION>ZIggy</option>
 <OPTION>Iggy</option>
</select>
<INPUT type='button' value='Sort' onClick='SortListBox( this.form.myselect, 0, this.form.myselect.options.length-1 );'>
</form>
</body>
</html>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2565629
OOPS!  One little typo -- in the onClick of the sort button, change the last parameter from:  options.length-1    to just:  options.length   -- that should do it.
0
 

Author Comment

by:wariar
ID: 2565848
Thanks! It is what I was looking for.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question