I am trying to sort an array from lowest to highest value.
the example below shows it already sorted in this manner
var array = ["-394","-275","-156","-37","82","201","320","439","558","677","796"];
However when I do this:
var array = ["-394", "-275", "-156", "-37", "82", "201", "320", "439", "558", "677", "796"];
array.sort(function(a, b) {
return a.localeCompare(b, undefined, {
numeric: true
})
});
console.log(array);
This is returned (i'm not sure what sorting has occurred):
["-37", "-156", "-275", "-394", "82", "201", "320", "439", "558", "677", "796"]
I've looked at:
but it doesn't seem to mentioned anything specifically about handling negative numbers.
What is the correct way to sort an array of numbers that includes negative values?