您可以編寫一個函數由兩個標準來排序。
// ORDER BY EmValue, LiMinusEmText
function sortBoth(a, b) {
var aText = $(a).text().replace(/\(\d+\)\s*$/, ""); // chop off the bracket
var bText = $(b).text().replace(/\(\d+\)\s*$/, ""); // and numbers portion
var aValue = +$(a).find("em").text().replace(/\D/g, ""); // parse out em value
var bValue = +$(b).find("em").text().replace(/\D/g, ""); // and convert to number
if (aValue == bValue) {
if (aText == bText) {
return 0;
}
else if (aText < bText) {
return -1;
}
else /*if (aText > bText)*/ {
return 1;
}
}
else {
return aValue - bValue;
}
}
// ORDER BY LiMinusEmText, EmValue
function sortBoth(a, b) {
var aText = $(a).text().replace(/\(\d+\)\s*$/, ""); // chop off the bracket
var bText = $(b).text().replace(/\(\d+\)\s*$/, ""); // and numbers portion
var aValue = +$(a).find("em").text().replace(/\D/g, ""); // parse out em value
var bValue = +$(b).find("em").text().replace(/\D/g, ""); // and convert to number
if (aText == bText) { // strings value same?
return aValue - bValue; // then return a - b
}
else if (aText < bText) {
return -1;
}
else /*if (aText > bText)*/ {
return 1;
}
}
它顯示了您的示例中的Joe Rocket(58)和Salpinestars(58)(FF和Chrome)。如何重現問題? –
打開http://jsbin.com/aboca3/95/edit#javascript,html,live,點擊「... 19更多選擇」,然後點擊「...更少的選擇」。 – Jasper
「.sort(sortAlpha).sort(sortEm)」背後的想法是首先按字母排序,然後按中的數字排序。 – Jasper