Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a drop down list box

Posted on 2000-02-28
3
Medium Priority
?
231 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 200 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

Industry Leaders: 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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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