how to sort array numbers in ascending order in javascript
// how to sort array numbers in javascript without mutating original array.const numbers =[100,25,1,5];const sorted = numbers.slice().sort((a, b)=> a - b);// returns a new sorted arrayconsole.log(numbers);// [100, 25, 1, 5]console.log(sorted);// [1, 5, 25, 100]
//sort is function in js which sort according to alphanumerical//for array with number it is little twistconst items=["Banana","Orange","Apple"];const ratings =[92,52,2,22]console.log(items.sort())// reuturn ["Apple","Banana","Orange]//for array with number
ratings.sort(function(a,b){return a-b;//ascending for decending b-a// return is negative a is sorted befor b // positive b is sorted before a// if they are the same is 0 then nothing changes.})
// a very fast sort implementationvar numArray =newFloat64Array([140000,104,99]);
numArray = numArray.sort();console.log(numArray)// to convert Float64Array to standard Array:Array.from(numArray);