The problem is that vA
and vB
could either or both result in NaN. I haven't found any '<' or '>' statement where comparing against NaN returns true. (MDN says, "NaN is not equal to anything, including NaN").
You can simply replace your NaN with the high value you used in the solution you posted, or you could do something more creative using NaN values.
function sortUsingNestedText(parent, childSelector, keySelector) {
var items = parent.children(childSelector).sort(function(a, b) {
var vA = parseInt($(keySelector, a).text().replace('1:','').replace(' ','')); // the replace actions removes spaces and the '1:' in the scale, parsInt makes sure it's a number
var vB = parseInt($(keySelector, b).text().replace('1:','').replace(' ',''));
if (isNaN(vA)) { vA = 9999999999; }
if (isNaN(vB)) { vB = 9999999999; }
return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
});
Or maybe something like this:
function sortUsingNestedText(parent, childSelector, keySelector) {
var items = parent.children(childSelector).sort(function(a, b) {
var vA = parseInt($(keySelector, a).text().replace('1:','').replace(' ','')); // the replace actions removes spaces and the '1:' in the scale, parsInt makes sure it's a number
var vB = parseInt($(keySelector, b).text().replace('1:','').replace(' ',''));
if (isNaN(vA) && !isNaN(vB)) { return 1; }
if (isNaN(vB) && !isNaN(vA)) { return -1; }
if (isNaN(vA) && isNaN(vB)) { return 0; }
return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
});