Please can anyone help me. I am able to sort an array into either ascending or descending order but I want to be able to sort the array above a numeric value.
For example if I have an array:
a0 = new Array(8, 1, 3, 9, 0, 0, 0, 0);
// Sort a0 to end up with;
// 1, 3, 8, 9, 0, 0, 0, 0;
I have included a fiddle: Basic template
I can only get it to work but with the zeros at the front of the sorted array
// 0, 0, 0, 0, 1, 3, 8, 9;
Thanks.
[8, 1, 3, 9, 0, 0, 0, 0].sort((a, b) => a > 0? a - b : +Infinity )
.[1, 3, 8, 9, 0, 0, 0, 0]
, which is what the OP asked for. If there are other requirements, the OP needs to state them. Anyway, it's just an example of a sort function that fits the OP.[ 0, 0, 0, 0, 1, 3, 8, 9 ]
as result ofconsole.log([8, 1, 3, 9, 0, 0, 0, 0].sort((a, b) => a > 0? a - b : +Infinity ))