In this tutorial, I will explain how to sort arrays in TypeScript using the built-in sort()
method and custom comparison functions. As a TypeScript developer, you will get requirements to sort arrays, and TypeScript provides a convenient way to handle this. We’ll cover sorting arrays of primitive types and objects and how to use arrow functions for concise code.
Sort Arrays of Primitive Types in TypeScript
TypeScript’s built-in sort()
method allows you to sort arrays of primitive types like numbers and strings. By default, the sort()
method sorts elements in ascending order.
Here’s an example of sorting an array of numbers:
const numbers: number[] = [4, 2, 5, 1, 3];
numbers.sort();
console.log(numbers); // Output: [1, 2, 3, 4, 5]
Here is the exact output in the screenshot below:

Here’s an example of sorting an array of strings:
const names: string[] = ['John', 'Alice', 'Bob', 'Emma'];
names.sort();
console.log(names); // Output: ['Alice', 'Bob', 'Emma', 'John']
As you can see, the sort()
method modifies the original array and returns the sorted array in ascending order.
Here is the exact output in the screenshot below:

Check out Filter Arrays in TypeScript
Sort Arrays of Objects
When sorting arrays of objects, you need to provide a comparison function to determine the sorting order. The comparison function takes two parameters (a and b) and returns a value indicating their relative order:
- If the function returns a negative value, a should be sorted before b.
- If the function returns a positive value, b should be sorted before a.
- If the function returns 0, a and b are considered equal in terms of sorting order.
Let’s say we have an array of objects representing people:
interface Person {
name: string;
age: number;
}
const people: Person[] = [
{ name: 'John', age: 30 },
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 35 },
{ name: 'Emma', age: 28 },
];
To sort the array based on the ‘age’ property, we can provide a comparison function:
people.sort((a, b) => a.age - b.age);
console.log(people);
/* Output:
[
{ name: 'Alice', age: 25 },
{ name: 'Emma', age: 28 },
{ name: 'John', age: 30 },
{ name: 'Bob', age: 35 }
]
*/
Read Arrays of Objects in TypeScript
Using Arrow Functions
TypeScript supports arrow functions, which provide a concise syntax for writing comparison functions. Arrow functions are particularly useful when sorting arrays of objects.
Here’s an example of sorting the ‘people’ array by name using an arrow function:
people.sort((a, b) => a.name.localeCompare(b.name));
console.log(people);
/* Output:
[
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 35 },
{ name: 'Emma', age: 28 },
{ name: 'John', age: 30 }
]
*/
The localeCompare()
method is used to compare strings based on their lexicographical order.
Sort in Descending Order
To sort arrays in descending order in TypeScript, you can modify the comparison function to reverse the order of comparison:
const numbers: number[] = [4, 2, 5, 1, 3];
numbers.sort((a, b) => b - a);
console.log(numbers); // Output: [5, 4, 3, 2, 1]
Read How to Filter an Array of Objects in TypeScript?
Sort Arrays in TypeScript Real-World Example
Let’s consider a real-world example where we have an array of user objects representing data of users from the USA. Below is the TypeScript code, you can write to sort arrays in TypeScript.
interface User {
firstName: string;
lastName: string;
age: number;
city: string;
}
const users: User[] = [
{ firstName: 'John', lastName: 'Doe', age: 32, city: 'New York' },
{ firstName: 'Emma', lastName: 'Smith', age: 28, city: 'Los Angeles' },
{ firstName: 'Michael', lastName: 'Johnson', age: 40, city: 'Chicago' },
{ firstName: 'Olivia', lastName: 'Williams', age: 35, city: 'Houston' },
];
Suppose we want to sort the users based on their last name. We can achieve this using the following code:
users.sort((a, b) => a.lastName.localeCompare(b.lastName));
console.log(users);
/* Output:
[
{ firstName: 'John', lastName: 'Doe', age: 32, city: 'New York' },
{ firstName: 'Michael', lastName: 'Johnson', age: 40, city: 'Chicago' },
{ firstName: 'Emma', lastName: 'Smith', age: 28, city: 'Los Angeles' },
{ firstName: 'Olivia', lastName: 'Williams', age: 35, city: 'Houston' }
]
*/
Conclusion
In this tutorial, we covered how to sort arrays in TypeScript using the built-in sort()
method and custom comparison functions. I have also explained sorting arrays of primitive types, objects, and using arrow functions for concise code. We also looked at a real-world example of sorting user data based on last name.
I hope now you understand how to use the sort()
method and comparison functions to efficiently sort arrays in your TypeScript projects.
You may also like:
I’m Bijay Kumar Sahoo, and I am honored to be awarded the Microsoft MVP. With over 18 years of experience in the IT industry, I got a chance to work on SharePoint Framework (SPFx) development, TypeScript, React, JavaScript, etc. My journey has taken me through esteemed organizations such as TCS, HP, and KPIT, where I have honed my skills and expanded my expertise. Check out more about me here.