I have the following array defined in JavaScript,
var myPriceList = [0, 0, 0, 1, 5, 3]
I need to sort the numbers in an ascending order AND so that 0s show at the end of the sort, like so –
var myPriceList = [1, 3, 5, 0, 0, 0]
I understand I can write a custom function like the one below but I’m just not sure how to incorporate the exception for “0”.
function sortNumber(a,b)
{
return b - a;
}
If
numSort(a,b)returns a value>0, then the index ofashould be higher (and thus later in the sequence). If the value is<0, thenbshould be later in the sequence.0means both are equal in terms or order.JSFiddle Demo. See also MDN sort reference.