Solved

Sorting a drop down list box

Posted on 2000-02-28
3
196 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now