afewminutesofcode.com

How to sort an array alphabetically in javascript

Cover Image for How to sort an array alphabetically in javascript

To sort an array alphabetically in javascript you can use the sort function.

To Sort a-z.

["blue", "red", "green", "yellow", "purple"].sort();
//outputs ["blue", "green", "purple", "red", "yellow"]

This works well but what if there is a mix between lower case and uppercase? eg

["blue", "Green", "red", "green", "yellow", "purple", "Silver"].sort();
//outputs ["Green", "Silver", "blue", "green", "purple", "red", "yellow"]

Despite what you might expect the sort method sorts alphabetically but has the capitalised letters words before the lowercase letters.

A more robust solution which will also handle non standard letters such as é is to use the localeCompare.

So now

["blue", "Green", "red", "green", "yellow", "purple", "Silver"].sort((a, b) =>
  a.localeCompare(b)
);
//outputs ["blue", "green", "Green", "purple", "red", "Silver", "yellow"]

As discussed in our article on sorting numerically there is also another syntax which you might come across which can be a bit cleaner and reusable.

const sortAtoZ = (a, b) => a.localeCompare(b);
const sortZtoA = (a, b) => b.localeCompare(a);

This will allow us to pass in our function directly into the sort.

eg

console.log(
  ["blue", "Green", "red", "green", "yellow", "purple", "Silver"].sort(sortAtoZ)
);
//outputs ["blue", "green", "Green", "purple", "red", "Silver", "yellow"]
console.log(
  ["blue", "Green", "red", "green", "yellow", "purple", "Silver"].sort(sortZtoA)
);
//outputs ["yellow", "Silver", "red", "purple", "Green", "green", "blue"]

I find this the best solution because we are able to test this function and reuse it with confidence across our codebase, also by giving a descriptive name to the function we help prevent getting our orders mixed up.

You might also like

Cover Image for Check if a string contains a substring in JavaScript

Check if a string contains a substring in JavaScript

Since ES6 you can now use the built in includes method eg string.includes(substring).

Cover Image for Create a Cricket Points Table in Javascript - Part 5

Create a Cricket Points Table in Javascript - Part 5

In this tutorial we are going to build our points table based off our previous work.

Cover Image for Create a Cricket Points Table in Javascript - Part 4

Create a Cricket Points Table in Javascript - Part 4

In this tutorial we are going to complete the rest of the calculations required for the points table.

Cover Image for Create a Cricket Points Table in Javascript - Part 3

Create a Cricket Points Table in Javascript - Part 3

In this tutorial we are going to create functions to calculate the total number of wins, losses, no results and points a team has for the tournament.

Cover Image for Create a Cricket Points Table in Javascript - Part 2

Create a Cricket Points Table in Javascript - Part 2

In this tutorial we are going to create a function to calculate when a team has won, lost or there is no result.

Cover Image for Create a Cricket Points Table in Javascript - Part 1

Create a Cricket Points Table in Javascript - Part 1

In this tutorial we are going to Create a Cricket Points Table in JavaScript.

Cover Image for Create a weather voice assistant in React

Create a weather voice assistant in React

In this tutorial we are going to create a simple React app that will use the speechSynthesis API to speak the current temperature in the city we type in.

Cover Image for Getting to know the many voices in your browser

Getting to know the many voices in your browser

The Speech Synthesis API has been around since about 2014 but still has not gained a huge amount of traction on many sites.

Cover Image for How to create a custom sort order in javascript

How to create a custom sort order in javascript

When sorting an array of objects in JavaScript, usually we will want to sort alphabetically or numerically, but there are also some cases were we might need a custom sort order.

Cover Image for How to sort an array alphabetically in javascript

How to sort an array alphabetically in javascript

To sort an array alphabetically in javascript you can use the sort function.

Cover Image for How to sort an array numerically in javascript

How to sort an array numerically in javascript

To sort an array in javascript use the sort method sort((a, b) => a - b)

Cover Image for How to convert an array into an object in javascript

How to convert an array into an object in javascript

To convert an array into an object we will create a function and give it 2 properties, an array and a key..

Cover Image for How to filter an array in javascript

How to filter an array in javascript

To filter an array in Javascript we can pass in a condition to the built in filter function.