I am looking for a function that will take a list of options in a select list and sort them alphabetically but with a twist. All values with the text \'NA\' should be pushed to
If you have more than one select in the page (multiple selects) use this:
function NASort(a, b) {
if (a.innerHTML == 'NA') {
return 1;
}
else if (b.innerHTML == 'NA') {
return -1;
}
return (a.innerHTML > b.innerHTML) ? 1 : -1;
};
$('select').each(function( index ) {
$(this).find('option').sort(NASort).appendTo($(this));
});
demo: http://jsfiddle.net/4bvVz/
function NASort(a, b) {
if (a.innerHTML == 'NA') {
return 1;
}
else if (b.innerHTML == 'NA') {
return -1;
}
return (a.innerHTML > b.innerHTML) ? 1 : -1;
};
$('select option').sort(NASort).appendTo('select');
I've made some changes to the original function and it worked fine.
function NASort(a, b) {
console.log(a.innerHTML.substr(0, 1).toLowerCase() + ' > ' + b.innerHTML.substr(0, 1).toLowerCase());
return (a.innerHTML.substr(0, 1).toLowerCase() > b.innerHTML.substr(0, 1).toLowerCase()) ? 1 : -1;
};
$('select option').sort(NASort).appendTo('select');
var sb = $('select');
sb.append(sb.find('option').sort(function(a, b){
return (
a = $(a).text(),
b = $(b).text(),
a == 'NA' ? 1 : b == 'NA' ? -1 : 0|a > b
);
}));