Solved

Sorting a drop down list box

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
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 …

747 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

12 Experts available now in Live!

Get 1:1 Help Now