Numerical Sort in JavaScript

Write a program to sort numerical array in Javascript.

Example:

Input:  arr=[10,7,5,25,2,3]
Output: arr = [2,3,5,7,10,25]

Approach

JavaScript

var points = [10,7,5,25,2,3];

console.log("input:"+points);

//return (b-a) in case of descending order
points.sort(function(ab){return a-b})

console.log("output:"+points);


No comments:

Post a Comment