sort an array in descending order

Sometimes you might want to display data in an ascending order in your Javascript application.

Javascript does not have an explicit method that sorts in descending. However, what we can do is to first sort the array in descending order and then reverse it using the reverse() array method.

const cars = ["mazda", "bmw", "toyota", "mecerdes", "peugeot", "lexus", "acura", "bentley", "tesla"]
cars.sort().reverse().map((item) => {
  console.warn(item);
})

Hope it helps!

Author

Leave a Reply

Your email address will not be published. Required fields are marked *