Back
I am trying to get the highest and lowest value from an integer array when I apply the sort function it seems to handle the values as strings. Is there a way to get the sort function to actually sort on integer value?
By default, the sort method sorts elements alphabetically. So, you can do something like this:
var numArray=[14000,104,99];console.log(numArray.sort((a,b)=>a-b));//for ascending orderconsole.log(numArray.sort((a,b)=>b-a));//for descending order
var numArray=[14000,104,99];
console.log(numArray.sort((a,b)=>a-b));//for ascending order
console.log(numArray.sort((a,b)=>b-a));//for descending order
Want to be a full stack developer? Check out the full stack developer course from Intellipaat.
31k questions
32.8k answers
501 comments
693 users