This post will discuss how to in-place sort an array in JavaScript.

Sorting an array in JavaScript means arranging the elements of the array in a certain order, such as alphabetical, numerical, or custom. There are several ways to in-place sort an array in JavaScript, depending on the type of array.

1. Sort an integer array

The Array.sort() function sorts the elements of an array and returns the original sorted array, and no copy is made. For example, to sort an array of integers in ascending order, we can do like:

Download  Run Code

 
To sort an array according to a different criterion, we can pass a compare function as an argument to the sort() function. The comparison function takes two elements as parameters and return a negative, zero, or positive value depending on their order. For example, to sort an array of integers in descending order, we can pass a comparison function to define the sort order of elements.

Download  Run Code

 
Without the compare function, all values in the array will be converted to a string, and the comparison is made using the lexicographic order irrespective of the types of values in the array. The sort() function can be used with function expressions or arrow function expressions offering the shorter syntax. For example:

Download  Run Code

2. Sort a string array

If we try to sort a string array using the sort() function, it converts the elements to strings and compares them using their Unicode code point values. For example, to sort an array of strings alphabetically, we can do like:

Download  Run Code

 
However, this may not work well for strings that contain characters from different languages or alphabets, such as accented letters or symbols. We can use the localeCompare() function to compare two strings based on their language-sensitive collation rules. This function compares two strings and returns a negative, zero, or positive value depending on their order. We can use this function to sort an array of strings that contain characters from different languages or alphabets. For example, to sort an array of strings in French, we can use this compare function:

Download  Run Code

3. Sort an object array

To sort an array of objects using one or more of their properties, we need to provide a compare function as an argument to the sort() function. The compare function should take two objects as parameters and return a negative, zero, or positive value depending on their order. For example, we can sort an array of books by year like this:

Download  Run Code

Output:

[
  { title: "The Lord of the Rings", author: "J.R.R. Tolkien", year: 1954 },
  { title: "A Game of Thrones", author: "George R. R. Martin", year: 1996 },
  { title: "Harry Potter and the Philosopher's Stone", author: "J. K. Rowling", year: 1997 }
]

 
Alternatively, we can use the _.orderBy() function from Lodash library to sort an object array by multiple fields with less code and more flexibility. This function takes an array of objects and one or more properties to sort by as arguments, and returns a new array of objects sorted by the specified properties. We can also pass an optional array of orders to specify whether to sort in ascending or descending order. For example, we can sort an array of books by author in ascending order and then by year in descending order like this:

Download Code

Output:

[
  { title: "A Clash of Kings", author: "George R. R. Martin", year: 1998 },
  { title: "A Game of Thrones", author: "George R. R. Martin", year: 1996 },
  { title: "Harry Potter and the Goblet of Fire", author: "J. K. Rowling", year: 2000 },
  { title: "Harry Potter and the Philosopher's Stone", author: "J. K. Rowling", year: 1997 }
]

That’s all about in-place sorting an array in JavaScript.