Sort array in ascending or descending order using sort function

Array :

let k=[1,3,11,2,33,22]

k.sort()

Result :
[1,11,2,22,3,33]

Here the array just sorted based on the ASCI values, so how to make it such way that it can return the array in ascending sort order.

k.sort((a,b)=>{return a-b})
use the fat arrow function
Result : [1,2,3,11,22,33]

descending sort order.
k.sort((a,b)=>{return b-a})

Result : [33,22,11,3,2,1]

Use the below tool to try ES6 javascript

https://stephengrider.github.io/JSPlaygrounds/


/* androidsharp */

0 comments: