TypeScript – How to sort Array of Objects

  • by

Array.sort() function sorts an Array. The Sort() function will sort array using the optional compareFunction provided, if it is not provided Javascript will sort the array object by converting values to strings and comparing strings in UTF-16 code units order.

Syntax for sorting:

arrayName.sort(compareFunction);

compareFunction: If specified, Javascript will sort the array depending on the condition provided.

Example:
Below example shows sorting an array object in Typescript using property name in ascending and descending order respectively.

Creating Array object:

export class companies {
name: string;
revenue: number;
category: string
}

let company: companies[] = [];

company.push({
name: "Samsung",
revenue: 300.00,
category: "Electronics"
});

company.push({
name: "LG",
revenue: 450.00,
category: "Electronics"
});

company.push({
name: "Apple",
revenue: 298.00,
category: "Mobile Industry"
});

company.push({
name: "HP",
revenue: 758.00,
category: "Computer Electronics"
});

company.push({
name: "Dell",
revenue: 423.00,
category: "Computer Electronics"
});

company.push({
name: "Acer",
revenue: 623.00,
category: "Electronics"
});

We are here creating an Array object company[] of type companies.
Sorting the array using name of the company in ascending order:

let sortedCompany = company.sort((a, b) => (a.name < b.name) ? -1 : 1);
console.log(JSON.stringify(sortedCompany));

Output:

[{"name":"Acer","revenue":623,"category":"Electronics"},{"name":"Apple","revenue":298,"category":"Mobile Industry"},{"name":"Dell","revenue":423,"category":"Computer 
Electronics"},{"name":"HP","revenue":758,"category":"Computer Electronics"},{"name":"LG","revenue":450,"category":"Electronics"},{"name":"Samsung","revenue":300,"category":"Electronics"}]

Sorting using name of the company in descending order:

let sortedCompany = company.sort((a, b) => (a.name > b.name) ? -1 : 1);
console.log(JSON.stringify(sortedCompany));

Output:

[{"name":"Samsung","revenue":300,"category":"Electronics"},{"name":"LG","revenue":450,"category":"Electronics"},{"name":"HP","revenue":758,"category":"Computer Electronics"},{"name":"Dell","revenue":423,"category":"Computer Electronics"},{"name":"Apple","revenue":298,"category":"Mobile Industry"},{"name":"Acer","revenue":623,"category":"Electronics"}]

Now let’s sort using the numeric value:
In this example, revenue is a numeric type, we will use this property to sort our array object:

Example:

let sortedCompany = company.sort((a, b) => (a.revenue > b.revenue) ? -1 : 1);
console.log(JSON.stringify(sortedCompany));

Output:

[{"name":"HP","revenue":758,"category":"Computer Electronics"},{"name":"Acer","revenue":623,"category":"Electronics"},{"name":"LG","revenue":450,"category":"Electronics"},{"name":"Dell","revenue":423,"category":"Computer Electronics"},{"name":"Samsung","revenue":300,"category":"Electronics"},{"name":"Apple","revenue":298,"category":"Mobile Industry"}]


  1. Base64Encode.io - Realtime Encode your string to Base64 format.
  2. Base64Decode.io - Realtime Decode Base64 data to plain text.
  3. Md5Hash.io - Realtime Encoding in Md5Hash.