Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sorting a drop down list box

Posted on 2000-02-28
3
Medium Priority
?
229 Views
Last Modified: 2010-04-09
How can I sort a combo box after adding items to it dynamically?
0
Comment
Question by:wariar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

This article discusses how to create an extensible mechanism for linked drop downs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
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 …

688 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