softare development

Sort an Array in Descending Order in JavaScript

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!

Recent Posts

Essential VS Code Extensions Every Developer Should Use

Visual Studio Code (VS Code) is powerful out of the box, but its real strength…

4 days ago

JavaScript Variables

1. What Is a Variable in JavaScript? A variable is a named container used to store data…

5 days ago

C++ Queue

1. What Is a Queue? A Queue is a linear data structure that follows the principle: FIFO – First…

6 days ago

Must-Know Angular Concepts

Angular is a full-featured frontend framework built by Google for creating large, maintainable, and high-performance web applications.…

1 week ago

Responsive Web Design (RWD)

What Is Responsive Web Design? Responsive Web Design (RWD) is an approach to building websites…

1 week ago

Geolocation API in JavaScript

The Geolocation API allows a web application to access a user’s geographical location (latitude, longitude, and more), with…

2 weeks ago