Skip to content
the poor coder

sort array

Poor Man
Published
Reading time
2 min read
On this page

Sorting Arrays in JavaScript#

Sorting is a fundamental operation in programming. In JavaScript, we can sort arrays using the built-in sort() method.

Syntax#

The sort() method has the following syntax:

Text
array.sort([compareFunction])

The compareFunction parameter is optional. It is a function that defines the sort order. If omitted, the array is sorted in ascending order by default.

Examples#

Let's see some examples of sorting arrays in JavaScript.

Sorting an Array of Strings

Suppose we have an array of names:

Text
const names = ["Alice", "Bob", "Charlie", "David"];

We can sort this array in ascending order as follows:

Text
names.sort(); // ["Alice", "Bob", "Charlie", "David"]

The resulting array is sorted in alphabetical order.

Sorting an Array of Numbers

Suppose we have an array of numbers:

Text
const numbers = [5, 2, 9, 1, 7];

We can sort this array in ascending order as follows:

Text
numbers.sort((a, b) => a - b); // [1, 2, 5, 7, 9]

The compareFunction parameter is a function that takes two arguments a and b, which are two elements of the array being sorted. The function should return a negative, zero, or positive value, depending on the sort order of a and b. In this example, we subtract b from a, which sorts the array in ascending order.

We can also sort the array in descending order as follows:

Text
numbers.sort((a, b) => b - a); // [9, 7, 5, 2, 1]

In this case, we subtract a from b, which sorts the array in descending order.

Sorting an Array of Objects

Suppose we have an array of objects:

Text
const people = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 20 }
];

We can sort this array by the age property in ascending order as follows:

Text
people.sort((a, b) => a.age - b.age);

The resulting array is sorted by age as follows:

Text
[
  { name: "Charlie", age: 20 },
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 }
]

We can sort the array by the name property in alphabetical order as follows:

Text
people.sort((a, b) => a.name.localeCompare(b.name));

The resulting array is sorted by name as follows:

Text
[
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 20 }
]

Conclusion#

Sorting arrays is an important skill in programming, and JavaScript provides a convenient sort() method for this purpose. By providing a compareFunction parameter, we can customize the sort order to suit our needs.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC