How do you sort an array in JavaScript?

In Javascript, you can sort an array using the sort() method, which is a built-in method that sorts the elements of an array in place, meaning it modifies the original array.

The sort() method converts the elements of the array into strings and then sorts them based on their Unicode code points by default. However, this default behavior may not always yield the desired results when sorting arrays of numbers or non-string elements.

Therefore, it’s important to understand how to use the sort() method correctly and handle any specific sorting requirements.

Sorting an array of strings in alphabetical order:

If you have an array of strings and you want to sort them in alphabetical order, you can simply use the sort() method without any arguments.

const fruits = ["apple", "banana", "cherry", "date", "elderberry"];
fruits.sort();
console.log(fruits); // Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']

As you can see in above example, sort() method sorted the array of strings in alphabetical order.

Sorting an array of numbers in ascending order:

If you have an array of numbers and you want to sort them in ascending order, you need to provide a comparison function as an argument to the sort() method.

The comparison function should return a negative value if the first element is smaller than the second element, a positive value if the first element is larger than the second element, and 0 if the two elements are equal.

For example:

const numbers = [10, 5, 8, 1, 7];
numbers.sort(function (a, b) {
  return a - b;
});
console.log(numbers); // Output: [1, 5, 7, 8, 10]

Sorting an array of objects based on a specific property:

If you have an array of objects and you want to sort them based on a specific property, such as an age or a price, you can use a comparison function that accesses the property of each object.

For example:

const students = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 22 },
  { name: 'Charlie', age: 28 },
  { name: 'David', age: 20 }
];

students.sort(function(a, b) {
  return a.age - b.age;
});

console.log(students);
/*
Output:
[
  { name: 'David', age: 20 },
  { name: 'Bob', age: 22 },
  { name: 'Alice', age: 25 },
  { name: 'Charlie', age: 28 }
]

In the example above, the comparison function accesses the age property of each object and subtracts the age of a from the age of b. This will sort the objects in ascending order based on their age property, modifying the original array.

Sorting an array with custom sorting logic:

If you need to implement custom sorting logic that goes beyond simple comparisons, you can define a comparison function that returns a value based on your desired sorting criteria.

For example:

const items = [
  { name: "item1", price: 20 },
  { name: "item2", price: 15 },
  { name: "item3", price: 25 },
  { name: "item4", price: 10 },
];

items.sort(function (a, b) {
  // Custom sorting logic based on price and name
  if (a.price < b.price) return -1;
  if (a.price > b.price) return 1;
  if (a.name < b.name) return -1;
  if (a.name > b.name) return 1;
  return 0;
});

console.log(items);
/*
Output:
[
  { name: 'item4', price: 10 },
  { name: 'item2', price: 15 },
  { name: 'item1', price: 20 },
  { name: 'item3', price: 25 }
]
*/

In this example, the comparison function implements custom sorting logic based on both the price and name properties of the objects in the array. The sort() method will rearrange the elements in the array based on the custom sorting logic, modifying the original array.

Notes:

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: