AppDividend
Latest Code Tutorials

Javascript Array Sort Example | Array.prototype.sort() Tutorial

0

Javascript Array Sort Example | Array.prototype.sort() Tutorial is today’s leading topic. The sort() method sorts all the elements of the array in place and returns that array. The javascript sort() method can also accept the parameter like function. So it will sort an array based on that function logic. The default sort order is built on converting the item into strings and then comparing their sequences of UTF-16 code units values.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript Array Sort Example

The sort order can be either alphabetic or the numeric, and either the ascending (up) or descending (down). The syntax for sort() method is following. This method is not a pure function because the sort() method changes the original array. The syntax is following.

array.sort(compareFunction)

The compareFunction() parameter is an optional argument. The function that defines an alternative sort order and it should return a negative, zero, or positive value, depending on the arguments.

Let us take a simple example without compare function. Create an app.js file and add the code.

// app.js

let points = [4, 10, 100, 25, 2, 20];
points.sort();

console.log(points);

Run the file by typing the node app in the terminal. See the output, and it is not what you have expected.

 

Javascript Array Sort Example | Array.prototype.sort() Tutorial

Again, it does not merely put the values in ascending order. By default, the sort() method sorts the values as strings in alphabetical and ascending order.

Advance array sort() method example

Let us pass the compareFunction and change the default sort order.

// app.js

let points = [4, 10, 100, 25, 2, 20];
const sortAsc = (a, b) => a - b;
points.sort(sortAsc);

console.log(points);

So, here we are sorting the values in Ascending order, and we have written one JS function to do that. Run the file and see the output.

 

Javascript Array Sort Example

We can do the Descending order also.

// app.js

let points = [4, 10, 100, 25, 2, 20];
const sortAsc = (a, b) => b - a;
points.sort(sortAsc);

console.log(points);

If the compareFunction() is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.

// app.js

let freelance = ['Upwork', 'Freelancer', 'Truelancer', 'PeoplePerHour'];
freelance.sort();

console.log(freelance);

 

Array.prototype.sort() Tutorial

Sorting an array of objects

See the following example.

let apps = [
  { name: 'Amazon', value: 1 },
  { name: 'Amazon Prime', value: 21 },
  { name: 'Amazon Music', value: 10 },
  { name: 'Amazon Go', value: 100 },
  { name: 'Amazon Alexa', value: 13 }
];
apps.sort((a, b) => a.value-b.value);

console.log(apps);

Here, we have used the arrow function. Now, it will sort Ascending order based on the values.

 

Sorting an array of objects in Javascript

Sorting non-ASCII characters and localeCompare()

For sorting strings with the non-ASCII characters, i.e., strings with accented characters like these (e, é, è, a, ä, etc.), The strings from other languages apart from English: use the String.localCompare() method. The String.localCompare() function can compare those characters, so they appear in the right order.

// app.js

let words = ['réservé', 'premier', 'cliché', 'communiqué', 'café', 'adieu'];
words.sort(function (a, b) {
  return a.localeCompare(b);
});

console.log(words);

Finally, Javascript Array Sort Example | Array.prototype.sort() Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.