Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

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?

1 Answer

0 votes
by (13.1k points)

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 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. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...