coding

javascript

How to Sort an Array in Javascript

Explore the different ways to sort Arrays in Javascript

/How%20to%20Sort%20an%20Array%20in%20Javascript

How to sort an Array in javascript

To sort an array in ascending order:

    const list = [1,6,3,8,3,2]
    const sortedList = list
      .slice()
      .sort((a, b) => a.upvotes - b.upvotes);

To sort an array in descending order:

    const list = [1,6,3,8,3,2]
    const sortedList = list
      .slice()
      .sort((a, b) => b.upvotes - a.upvotes);