Javascript Sort — A Developer's Guide

February 11, 2026
const numbers = [4, 2, 7, 1, 3];
numbers.sort((a, b) => a - b);
console.log(numbers);
[1, 2, 3, 4, 7]

Sorting data - it's not the most glamorous task, but someone's gotta do it. And if you're working with JavaScript, you'll be doing it a lot. I mean, have you seen the state of some APIs' response data? It's like they want us to practice our sorting skills. Anyway, the good news is that JavaScript has a built-in `sort()` method that makes it relatively easy to sort arrays. In this example above, we're sorting an array of numbers in ascending order - simple enough.

How JavaScript Sort Works

The `sort()` method takes a compare function as an argument. This function is used to define the sort order. If the result of the compare function is less than 0, the first element will come first in the sorted array. If the result is greater than 0, the second element will come first. And if the result is equal to 0... well, you get the idea.

const strings = ['banana', 'apple', 'cherry'];
strings.sort((a, b) => {
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
});
console.log(strings);
['apple', 'banana', 'cherry']

Sorting Strings - Case Sensitivity

When sorting strings, you might want to ignore case sensitivity. I mean, who wants 'Zebra' to come before 'apple'? To achieve this, you can convert both strings to lowercase or uppercase before comparing them.

const strings = ['Zebra', 'apple', 'Cherry'];
strings.sort((a, b) => {
  const lowerA = a.toLowerCase();
  const lowerB = b.toLowerCase();
  if (lowerA < lowerB) return -1;
  if (lowerA > lowerB) return 1;
  return 0;
});
console.log(strings);
['apple', 'Cherry', 'Zebra']

Sorting Objects

This is where things can get a bit tricky. When sorting objects, you need to specify which property you want to sort by. Let's say we have an array of objects with `name` and `age` properties.

const people = [
  { name: 'John', age: 30 },
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 40 }
];
people.sort((a, b) => a.age - b.age);
console.log(people);
[{ name: 'Alice', age: 25 }, { name: 'John', age: 30 }, { name: 'Bob', age: 40 }]

Sorting Objects - Multiple Properties

Sometimes you want to sort by multiple properties. For example, first by `age`, and then by `name`. To achieve this, you can use a combination of compare functions.

const people = [
  { name: 'John', age: 30 },
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 25 }
];
people.sort((a, b) => {
  if (a.age !== b.age) return a.age - b.age;
return a.name.localeCompare(b.name);
});
console.log(people);
[{ name: 'Bob', age: 25 }, { name:'Alice' ,age :30},{name :'John' ,age :30}]
javascript sort meme

Common Gotchas

Ifyou've debugged this at2am — welcome tothe club! Don't worry though — with practice,you'll become a JavaScript sorting master. So go ahead and experimentwith different scenarios — remember that practice makes perfect!

A n dwh e ny ou n eed h el p converting your JavaScri pt c ode f r om onelanguage t oanot her- j ustvisitCodeConverter.co.

Related Articles