mattquantic
Programmer
Hi
I use 3 functions to sort a select list that's been populated by dynamically. (people can add and remove items)
Was all working great until I need to apply it to a list of water tank values.
My list (which I can't change) is:
10 Ltrs
25 Ltrs
50 Ltrs
75 Ltrs
100 Ltrs
250 Ltrs
I thought about maybe spiting each item into 3 objects in an array (id,number part, letter part) and a then sorting by sorting by the numbers part. Then re-creating the list from the sorted array.
Is this the right direction? If so - how would I do that?
Thanks,
Matt
------------------------------------------------------------
The three functions I use:
// sort function - ascending (case-insensitive)
function sortFuncAsc(record1, record2) {
var value1 = record1.optText.toLowerCase();
var value2 = record2.optText.toLowerCase();
if (value1 > value2) return(1);
if (value1 < value2) return(-1);
return(0);
}
// sort function - descending (case-insensitive)
function sortFuncDesc(record1, record2) {
var value1 = record1.optText.toLowerCase();
var value2 = record2.optText.toLowerCase();
if (value1 > value2) return(-1);
if (value1 < value2) return(1);
return(0);
}
function sortSelect(selectToSort, ascendingOrder) {
// default to ascending sort
if (arguments.length == 1) ascendingOrder = true;
// copy options into an array
var myOptions = [];
for (var loop=0; loop<selectToSort.options.length; loop++) {
myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
}
// sort array
if (ascendingOrder) {
myOptions.sort(sortFuncAsc);
} else {
myOptions.sort(sortFuncDesc);
}
// copy sorted options from array back to select box
selectToSort.options.length = 0;
for (var loop=0; loop<myOptions.length; loop++) {
var newoption = new Option(myOptions[loop].optText,myOptions[loop].optValue);
selectToSort.add(newoption);
}
}
I use 3 functions to sort a select list that's been populated by dynamically. (people can add and remove items)
Was all working great until I need to apply it to a list of water tank values.
My list (which I can't change) is:
10 Ltrs
25 Ltrs
50 Ltrs
75 Ltrs
100 Ltrs
250 Ltrs
I thought about maybe spiting each item into 3 objects in an array (id,number part, letter part) and a then sorting by sorting by the numbers part. Then re-creating the list from the sorted array.
Is this the right direction? If so - how would I do that?
Thanks,
Matt
------------------------------------------------------------
The three functions I use:
// sort function - ascending (case-insensitive)
function sortFuncAsc(record1, record2) {
var value1 = record1.optText.toLowerCase();
var value2 = record2.optText.toLowerCase();
if (value1 > value2) return(1);
if (value1 < value2) return(-1);
return(0);
}
// sort function - descending (case-insensitive)
function sortFuncDesc(record1, record2) {
var value1 = record1.optText.toLowerCase();
var value2 = record2.optText.toLowerCase();
if (value1 > value2) return(-1);
if (value1 < value2) return(1);
return(0);
}
function sortSelect(selectToSort, ascendingOrder) {
// default to ascending sort
if (arguments.length == 1) ascendingOrder = true;
// copy options into an array
var myOptions = [];
for (var loop=0; loop<selectToSort.options.length; loop++) {
myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
}
// sort array
if (ascendingOrder) {
myOptions.sort(sortFuncAsc);
} else {
myOptions.sort(sortFuncDesc);
}
// copy sorted options from array back to select box
selectToSort.options.length = 0;
for (var loop=0; loop<myOptions.length; loop++) {
var newoption = new Option(myOptions[loop].optText,myOptions[loop].optValue);
selectToSort.add(newoption);
}
}