I have this array of pairs in JS :
var my_array = [['line_1','2'],['line_2','0'],['line_3','5'],['line_4',''],['line_5','4'],]
var my_sorted_array =my_array.sort((a, b) => a[1] - b[1])
console.log(my_sorted_array)
For now to sort it by second item I am using th code below
But I would like to put the blank/empty and null values to the end.
Expected result :
[['line_1','2'],['line_5','4'],['line_3','5'],['line_2','0']['line_4','']]
Thanks
2
Answers
I validate the Carsten Massmann answer because it's shorter and easier than mine. Thanks to him.
Here is another way do to it that I finally found :
You could replace each
0
value withInfinity
while doing the sort comparisons: