PDA

View Full Version : is there any way to sort selectlist?


frontline
06-22-2003, 12:54 PM
Hello
as the topic says...
thanks

joh6nn
06-22-2003, 05:37 PM
there's no way that's built in, unfortunately. i have to assume that you mean you wanted it sorted alphabetically. if not, let me know, and i'll try and help you with your custom sort.

for alphabetical sorting, the following should work, but i haven't tested it

var a = new Array(), s = document.formName.selectBoxName.options. l=selectBox.length;

for (var i = 0; i < l; i++) {
a[i] = new Option(s[i].text,s[i].value);
}

function alpha(a,b) {
return (a.text < b.text) ? -1 : 1;
}
a.sort(alpha);

for (var i = 0; i < l; i++) {
s[i].text = a[i].text;
s[i].text = a[i].value;
}