Array sort in JavaScript

In JavaScript, the sort method is used to sort the elements of an array in place and return the sorted array. It provides a convenient way to arrange array elements in ascending or descending order based on their values. In this explanation, we'll cover the sort method and how it can be used for array sorting in JavaScript.

To create an array in JavaScript, you can read how to create an array in JavaScript .

What is a sort method in an array?

The sort method by default sorts array elements as strings based on their Unicode code points. It converts each element into a string and compares the strings to determine the order. For example, the array [10, 2, 5, 1] would be sorted as [1, 10, 2, 5] because the numbers are compared as strings.

For example:

let numbers = [10, 2, 5, 1];
numbers.sort();
console.log(numbers);

Here you can learn ways to sort an array:

1. Ascending Order

To sort numbers in ascending order, you can use a compare functionas an argument to the sort method. The compare function should return a negative value if the first argument should be sorted before the second argument, a positive value if the first argument should be sorted after the second argument, or zero if the two arguments are considered equal.

For example:

let numbers = [10, 2, 5, 1];
numbers.sort(function(a, b) {
    return a - b;
});
console.log(numbers);

In this example, the compare function subtracts b from a, which sorts the numbers in ascending order.

2. Descending Order

To sort numbers in descending order, you can simply reverse the order of the subtraction in the compare function.

For example:

let numbers = [10, 2, 5, 1];
numbers.sort(function(a, b) {
    return b - a;
});
console.log(numbers);

The sort method can also be used to sort strings in alphabetical order.

3. Alphabetical Order

The sort method can also be used to sort strings in alphabetical order.

For example:

let fruits = ['apple', 'banana', 'cherry'];
fruits.sort();
console.log(fruits);

Here, the sort method sorts the arrays in the alphabetical order and produces the output.

4. Unicode Code Points

By default, the sort method sorts strings based on their Unicode code points. If you want to sort strings in a specific locale, you can use the localeCompare function as a compare function.

For example:

let fruits = ['apple', 'banana', 'cherry'];
fruits.sort(function(a, b) {
   return a.localeCompare(b);
});
console.log(fruits);

In this example, the localeCompare function is used to compare the strings in the default locale, resulting in alphabetical sorting.

5. With Arrow function

The sort method can also be used with an arrow function for concise syntax.

For example:

let numbers = [10, 2, 5, 1];
numbers.sort((a, b) => a - b);
console.log(numbers);

You can also sort an array in JavaScript with the help of an arrow function used in JavaScript.

6. Using slice method

It's important to note that the sort method modifies the original array in place. If you want to sort a copy of the array without modifying the original, you can create a shallow copy of the array using the slice method and then apply the sort method.

For example:

let numbers = [10, 2, 5, 1];
let sortedNumbers = numbers.slice().sort();
console.log(sortedNumbers); // Output: [1, 2, 5, 10]
console.log(numbers); // Output: [10, 2, 5, 1]

In this example, the original numbers array remains unchanged, and the sorted array is stored in the sortedNumbers variable.

Conclusion

The sort method is a powerful tool for sorting array elements in JavaScript. There are different ways to sort an array. By using compare functions, you can customize the sorting behavior to suit your specific requirements. Understanding how to use the sort method effectively allows you to arrange array elements in the desired order and facilitate data manipulation and analysis.


Learn via Video Course

Javascript(English) Logo

Javascript(English)

72966

3 hrs