Categories
Featured Development Resources HTML/Javascript

JavaScript Sort

The easiest way to sort an array in JavaScript is to utilize the built in sort method.

In this article we will provide a set of JS examples showing how to perform the most common sorts.

Sorting Strings In Javascript

The simplest sort can be done on array of strings using the sort() method.

letters = ["M", "A", "N", "I", "A", "C", "D", "E", "V"]
letters.sort()
console.log(letters)
// ['A', 'A', 'C', 'D', 'E', 'I', 'M', 'N', 'V']

Reversing A Sort Of Characters In JavaScript

Reversing the sort is easy with the reverse() method.

letters = ["M", "A", "N", "I", "A", "C", "D", "E", "V"]
letters.sort()
letters.reverse()
console.log(letters)
// ['V', 'N', 'M', 'I', 'E', 'D', 'C', 'A', 'A']

Sorting An Array Of Numbers

Sorting numbers with the sort() method requires a function to be passed in, the following example shows how to sort an array of numbers in ascending order:

numbers = [40, 100, 1, 5, 25, 10];
numbers.sort((a, b) => a - b);
console.log(numbers)
// [1, 5, 10, 25, 40, 100]

Sorting Numbers In Descending Order

Sorting in descending order simply requires the reverse:

numbers = [40, 100, 1, 5, 25, 10];
numbers.sort((a, b) => b - a );
console.log(numbers)
// [100, 40, 25, 10, 5, 1]

Sorting Objects In JavaScript

Sorting objects is done by passing in the key for the value that you would like to sort.

objects = [ { name: 'first', value: 1},
            { name: 'third', value: 3},
            { name: 'second', value: 2}]
objects.sort((a, b) => a.value - b.value)
console.log(objects)
//[
//    { name: 'first', value: 1 },
//    { name: 'second', value: 2 },
//    { name: 'third', value: 3 }
//]   

Sorting Dates In JavaScript

Dates can be sorted similarly to numbers as they are converted to a number during sorting. This example shows how to sort dates in ascending order:

dateObjects = [{ name: 'first', date: new Date("11-5-1955") },
               { name: 'third', date: new Date("10-21-2015") },
               { name: 'second', date: new Date("10-21-1985") }]
dateObjects.sort((a, b) => a.date - b.date)
console.log(dateObjects)
//[
//     { name: 'first', date: 1955 - 11 - 05T08: 00: 00.000Z },
//     { name: 'second', date: 1985 - 10 - 21T07: 00: 00.000Z },
//     { name: 'third', date: 2015 - 10 - 21T07: 00: 00.000Z }
//]

Not In Place JavaScript Sort

Now you might have noticed that the sort() function performs the sort in place replacing the original array.

If you’d prefer to make a sorted copy of the array you can do so by making a deep copy of the original, and this can easily be done using the slice() command without passing in any parameters:

letters = ["M", "A", "N", "I", "A", "C", "D", "E", "V"]
letters2 = letters.slice().sort()
console.log(letters)
// ["M", "A", "N", "I", "A", "C", "D", "E", "V"]
console.log(letters2)
// ['A', 'A', 'C', 'D', 'E', 'I', 'M', 'N', 'V']

JavaScript Sort Time Complexity

The time complexity for the sort() method is O(n log n). The algorithm used depends on the runtime you’re using with more recent versions of Chrome and Node using Timsort.

Conclusion

Understanding sorting in JavaScript is essential, and with this article you can understand the basics and be able to easily perform most searches.