function compareOptionText(a,b) {
/*
* return >0 if a>b
* 0 if a=b
* <0 if a<b
*/
// textual comparison
return a.text!=b.text ? a.text<b.text ? -1 : 1 : 0;
// numerical comparison
// return a.text - b.text;
}
function sortOptions(list) {
var items = list.options.length;
// create array and make copies of options in list
var tmpArray = new Array(items);
for ( i=0; i<items; i++ )
tmpArray[i] = new
Option(list.options[i].text,list.options[i].value);
// sort options using given function
tmpArray.sort(compareOptionText);
// make copies of sorted options back to list
for ( i=0; i<items; i++ )
list.options[i] = new Option(tmpArray[i].text,tmpArray[i].value);
}
snippets |
December 26, 2011
Sort the OPTIONs in a SELECT menu
Leave a Reply Cancel reply
You must be logged in to post a comment.