HOWTO · TypeScript
How to Sort Array of Objects in TypeScript
This article shows how to sort array of objects in TypeScript.
On this page
Sort an array of typed objects with employeesList.sort((a, b) => a.empId - b.empId). The comparator returns a negative number when a belongs first, a positive number when b belongs first, and zero when the compared values are equal. Reverse the subtraction for descending order, or compare another property when the first key ties. sort() changes the original array; spread it into a new array first if you need to keep the original order.
Define an object type for the records
An interface describes the properties each record must provide. Here, each employee has a numeric empId and a string empDesignation. Using one shared type lets TypeScript check both the array and the comparator property names. The interface is a compile-time contract: it helps catch misspelled property names while you edit, but it does not validate data received from an external source at runtime. Keep the property types aligned with the values used by the comparator.
Create a TypeScript array of objects
Declare the array as Employee[] so each item must match the interface. In TypeScript, define interface Employee { empId: number; empDesignation: string } and declare const employeesList: Employee[] = [...]. The sample has two employees with the same empId; their designations give the final comparator a useful second key. Property names in the data and sorting callback must match exactly. Each object literal is checked against Employee, so an omitted required property or a value of the wrong type is reported during type checking. The executable example below uses JavaScript-compatible object literals so it can run directly in Node.js; the TypeScript annotations belong in your .ts source file.
Sort objects by one or more properties
The numeric comparator a.empId - b.empId sorts IDs in ascending order and naturally returns zero for a tie. For descending order use b.empId - a.empId. To sort by a second property, return the ID comparison when it is nonzero, then compare empDesignation when IDs match. A comparator must return zero for records equal on both keys; returning -1 for every tie can give the sorting algorithm an inconsistent ordering rule. For other string fields, use a string comparison rather than subtraction. localeCompare() is a convenient option when human language ordering matters; its result can depend on locale, so choose an explicit locale if users need consistent regional ordering. If a property may be absent, decide how those records should rank and handle that case before comparing the values.
Run this example:
const employeesList = [
{ empId: 20, empDesignation: "senior engineer" },
{ empId: 14, empDesignation: "junior engineer" },
{ empId: 14, empDesignation: "developer" },
{ empId: 25, empDesignation: "program manager" },
{ empId: 12, empDesignation: "director" },
];
const byIdAscending = [...employeesList].sort((a, b) => a.empId - b.empId);
const byIdDescending = [...employeesList].sort((a, b) => b.empId - a.empId);
const byIdThenDesignation = [...employeesList].sort((a, b) =>
a.empId - b.empId || a.empDesignation.localeCompare(b.empDesignation)
);
console.log(byIdAscending.map((employee) => employee.empId).join(", "));
console.log(byIdDescending.map((employee) => employee.empId).join(", "));
console.log(byIdThenDesignation.map((employee) => `${employee.empId}: ${employee.empDesignation}`).join(" | "));
12, 14, 14, 20, 25
25, 20, 14, 14, 12
12: director | 14: developer | 14: junior engineer | 20: senior engineer | 25: program manager
The spread syntax makes each sort operate on a copy, leaving employeesList in its declared order. The first two lines display IDs in opposite directions. The final line displays each ID with its designation, applying the designation comparison only to employees whose IDs match. In a real application, choose a secondary property that makes tied records meaningful to your reader; add further comparisons with the same pattern when needed.