- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sort an array of integers correctly in JavaScript?
To sort an array of integers, use sort() in JavaScript.
Example
Following is the code −
var arrayOfIntegers = [67, 45, 98, 52]; arrayOfIntegers.sort(function (first, second) { return first - second; }); console.log(arrayOfIntegers);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo310.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo310.js [ 45, 52, 67, 98 ]
Advertisements