DelphiFAQ Home Search:

How to insert a string into a sorted SELECT listbox

 

comments43 comments. Current rating: 3 stars (34 votes). Leave comments and/ or rate it.

Question:

I have a sorted listbox and want to insert a new string without a complete resort.

Answer:

Use the javascript function shown below - pass the string that you want to insert as an argument. The name of the SELECT listbox is hard-coded in the script as 'mylist'.

Call it like this:
insertSortedLb('New Name')

The case is case sensitive and sorts alphabetically.
That means if you have numbers in the listbox, and these numbers include
10, 2, 300
then it will sort these numbers as
10, 2, 300
NOT as
2, 10, 300

function insertSortedLb(sVal) {
var lb = document.getElementById('mylist');

oOption = document.createElement("OPTION");
oOption.text = '';
oOption.value = '';
oOption.selected = false;
lb.options[lb.length] = oOption;

for(i=lb.length-1; i>0; i--)  {
  if (lb.options[i-1].text < sVal) {
    oOption = document.createElement("OPTION");
    oOption.text = sVal;
    oOption.value = sVal;
    oOption.selected = false;
    lb.options[i] = oOption;
    break;
  }
  else {
    lb.options[i].text = lb.options[i-1].text;
    lb.options[i].value = lb.options[i-1].value;
  }
} // for
} // end of insertSortedLb()

Comments:

You are on page 1 of 3, other pages: [1] 2 3
2006-09-05, 18:56:58
anonymous from United Kingdom  
try running this code with no items, one item, or two items - it doesn't work...
2006-10-05, 09:56:00
anonymous from Czech Republic  
It didn't work for me at all, I wrote my own function (tested only in IE6):

function insertToSelectSorted(selectElement, text, value)
{
var foundIndex = selectElement.options.length;

for (i = 0; i < selectElement.options.length; i++)
{
if (selectElement.options(i).innerText.toLowerCase() > text.toLowerCase())
{
foundIndex = i;
break;
}
}

var oOption = document.createElement('OPTION');
selectElement.add(oOption, foundIndex);
oOption.innerText = text;
oOption.value = value;
}
2007-08-16, 14:12:58
anonymous from United States  
Actually the above example is wrong in a couple places...

options(i) is not right, it must be options[i]

also, selectElement.add(x,y) is not valid, must do selectElement.options.add(x,y)

also, it's much easier to use new Option(text,value) than document.createElement('option') and then set text and value.

other than that... nice algorithm

function insertToSelectSorted(selectElement, text, value)
{
var foundIndex = selectElement.options.length;

for (i = 0; i < selectElement.options.length; i++)
{
if (selectElement.options[i].innerText.toLowerCase() > text.toLowerCase())
{
foundIndex = i;
break;
}
}

var oOption = new Option(text,value);
selectElement.options.add(oOption, foundIndex);

}
2007-09-04, 21:01:57
Justice from United States  
rating
This is a good script and very helpful, however if your list has non-English characters then you may get unexpected results. For example if your trying to insert 'Charlie' into the following list: {'Adam, 'Åsa', 'Bill', 'Dave'} the result will actually be {'Adam, 'Åsa', 'Charlie', 'Bill', 'Dave'} because the Å character is > C. Those pesky foreigners and their exotic letters...
2009-04-15, 01:41:04
anonymous from Canada  
what you guy did only for IE,

here is alternative version

http://www.mredkj.c..l005.html

2012-06-18, 21:44:38
anonymous from Colombia  
One alleged victim would routinely throw away his underwear to hide [url= http://www.louisvui..net/]Louis Vuitton Bags[/url] the supposed abuse. Week two of the trial began with a final prosecution witness. The mother of Victim No. 9 took the [url= http://www.chanelou..et/]Chanel Purses[/url] stand Monday to detail her son’s cover-up of the alleged sordid acts he suffered at Sandusky’s hands. “I [url= http://www.guccibel..rg/]Hermes Belts[/url] always wondered why he never had any underwear in the laundry,” she testified. “He just told me he had an [url= http://www.beltsout...org/]Mens Belts[/url] accident and he threw them out.” Indeed, she testified that he started becoming “sick” more often after [url= http://www.topbelts..org/]Gucci Belts[/url] spending time with the ex-Penn State coach. Victim No. 9 supposedly slept over at the Sandusky residence more than 100 times.Victim No. 9 testified [url= http://www.louisvui..net/]Louis Vuitton Handbags[/url] last Thursday that he screamed during one episode of alleged abuse, hoping that [url= http://www.beltsonl..com/]Louis Vuitton Belts[/url] Sandusky’s wife, who he said was always upstairs, would hear. “I think the basement is soundproof,” he said [url= http://www.chanels-..om/]Chanel Handbags[/url] tearfully, noting that Sandusky’s wife Dottie never did come down.Lead defense attorney Joe Amendola has also [url= http://www.chanelba..om/]Chanel Handbag[/url] suggested previously that the accusers and the Attorney General’s office might be colluding. Lead prosecutor Joe [url= http://www.lvshopos..net/]Louis Vuitton Purses[/url] McGettigan has often queried the alleged victims while on the stand: “Have I ever, ever, ever told you what to say?” which they’ve answered with resounding denials. But it could be a larger issue for Kline and the other subpoenaed attorneys to defend themselves against a [url= http://www.louisvui..com/]Louis Vuitton Purse[/url]
charge of violating the gag order issued by Judge Cleland, which bars any representative from either side from discussing the case with the media.


2012-08-31, 03:52:26
anonymous from Mumbai, India  
rating
i this is the best one
2012-11-12, 03:53:19
anonymous from United States  
2013-02-06, 04:14:10
 
rating
yppxhauy <a href=' http://viagraonlinetd.com/ '>buying viagra online</a> >:]] <a href=' http://cialisonlinetd.com/ '>cialis</a> =-]
2013-02-09, 09:26:13
 
rating
lxrtqjbn <a href=' http://paydayloansvba.co.uk/ '>short term loan</a> :-O <a href=' http://paydayloansvba.com/ '>fast payday loan</a> 2358 <a href=' http://paydayloansvba.ca/ '>cash advance online</a> 3161
2013-03-03, 23:47:16
 
rating
rjolged <a href=' http://cheapviagrafsb.com/ '>buy cheap viagra online</a> 8]]] <a href=' http://cheapcialisfsb.com/ '>cheap cialis</a> GVCeI
2013-04-02, 00:35:44
 
rating
urmvttaa http://viagraonlinedsb.com/ order viagra online >:-OOO http://cialisonlinedsb.com/ cialis nvLvm
2013-04-03, 08:21:53
 
rating
wxuosf http://cheapviagradsb.com/ cheap viagra 8764 http://cheapcialisdsb.com/ cheap cialis sale online VWYdQ
2013-04-04, 03:19:16
 
rating
omfpyyks http://paydayloansusafsh.com/ payday loans online 3097 http://paydayloansa..iafsh.com/ payday loans australia :-O http://paydayloansc..adafsh.ca/ payday loans online aGDdPw http://paydayloansu..fsh.co.uk/ payday loans 4975
2013-04-06, 04:59:32
 
rating
You are on page 1 of 3, other pages: [1] 2 3

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: