The Complete Guide to Sorting Arrays of Objects in Typescript (2024)

When we create an application or websites we need to sort array data frequently. And in Typescript we often need to sort arrays with strings, numbers or Booleans. But, there may be also the case when we need to sort more complex arrays of objects and not just the plain values.

Sorting is a great thing and it allows you to quickly find, compare and rank objects in any array based on your condition. Sorting array of objects allows us to order the objects based on one or most of their properties.

Lets take an example. Suppose we have an array of person objects with properties name, age and job. Now, sorting them alphabetically by name would order all the People with similar names together. And similarly, sorting by age from lowest or highest (or vise versa) will order them accordingly.

Here are most common use cases that i have came across for sorting arrays of objects in TypeScript.

  • Ordering and Organizing fetched data from an API to be more readable
  • Ranking some items to show the most relevant first
  • Putting items in alphabetical, numerical, or desired order
  • Sorting DOM elements to dynamically update the UI
  • Sorting products by price or rating for an ecommerce site

There are other many real world complex objects that you may come across and need sorting. Here is the full guide that covers most basic and most advance techniques for sorting array of objects with Typescript.

Basic Array Sorting in Typescript

As you may know already, the easiest way to sort an array in Typescript is using the built-in .sort() method. This method allows to sort elements of array and mutate the original array.

For example:

let numbers = [5, 3, 2, 4, 1];numbers.sort();console.log(numbers); // [1, 2, 3, 4, 5]

.sort() method sorts values as strings by default. This works flawlessly for sorting numbers and strings but can show incorrect results when you try to sort other data types.
You can customize the way of sorting elements by function .sort(). The compare function takes two arguments (a and b) and :

  • It will return a negative number if a should come before b
  • It will return a positive number if b should come before a
  • It will return 0 if a and b are equal

Here is an example to sort numbers from smallest to largest:

let numbers = [5, 3, 2, 4, 1];numbers.sort((a, b) => a - b);console.log(numbers); // [1, 2, 3, 4, 5]

The above example passes a compare function that subtracts b from a, and returns a negative number if a is less than b. This compare function gives you a lot of flexibility in how arrays are sorted in Typescript.

Sorting Arrays of Primitives

You can use the sort() method for sorting arrays of primitive values like strings, numbers, and booleans in TypeScript.
Here is an example to sort an array of strings:

let strings = ['c', 'a', 'b'];strings.sort();// ['a', 'b', 'c']

In above the sort() method will sort the strings alphabetically in ascending order by default.

You can similarly sort the numbers too.

let numbers = [3, 1, 2];numbers.sort();// [1, 2, 3]

This will sort the numbers in ascending order as well.

Note: For descending sorts, you can pass a compare function to sort() - we’ll cover this more in the “Sorting in Descending Order” section below.

Sorting Arrays of Objects

Objects don’t have a default sort order like strings or numbers. You will need to provide a compare function that specifies which property to sort on when sorting arrays of objects in TypeScript. With this compare function you will tell TypeScript how to compare the objects.

Here is an example:

interface Person {name: string;age: number;}const people: Person[] = [{name: "Jack", age: 30},{name: "Mary", age: 25},{name: "John", age: 40}];people.sort((a, b) => {return a.age - b.age;});

In this example we are sorting the array of Person objects by the age property in ascending order. This compare function subtracts the age of person A from person B to get a positive or negative number to find out the sorting order. This allows you full control over how TypeScript should sort an array of objects.

Sort by a Single Property

You can sort an array of objects by a single property like name or by age. In TypeScript the Array.sort() method accepts a compare function through which you can compare the name or age property of objects.

For example:

interface Person {name: string;age: number;}const people: Person[] = [{name: "John", age: 20},{name: "Mary", age: 25},{name: "Mike", age: 30}];people.sort((a, b) => {if(a.name < b.name) return -1;if(a.name > b.name) return 1;return 0;});console.log(people);// [// {name: "John", age: 20},// {name: "Mary", age: 25},// {name: "Mike", age: 30}// ]

In this example the compare function in sort method takes two Person objects a and b. It compares the name property of a and b alphabetically and then it returns -1 if a should come before b, 1 if a should come after b and 0 if a and b are equal.

Similarly, You can sort by any property like age or date of birth. To do that you will only need to change the compare function property.

Sort by Multiple Properties

Also, You can sort any array of objects by multiple properties. For example, if you need to sort the array by name first, then by age:

interface Person {name: string;age: number;} const people: Person[] = [{name: "Bob", age: 30},{name: "Alice", age: 25},{name: "Bob", age: 20}]; people.sort((a, b) => {if (a.name < b.name) return -1;if (a.name > b.name) return 1;if (a.age < b.age) return -1;if (a.age > b.age) return 1; return 0;}); console.log(people);// [// {name: "Alice", age: 25},// {name: "Bob", age: 20},// {name: "Bob", age: 30}// ]

This example first compares the name property and then sort it alphabetically. When it is done with sorting the names, it will compare the age property and sort them numerically.

So, you can add as many comparison scenarios as you need like above. The array will be sorted by each property in order.

Sorting in Descending Order

If you need to sort an array of objects in descending order, you have to provide a compare function to the sort() method that reverses the default sort order.

Here is an example:

interface User {name: string;age: number;} const users: User[] = [{name: "John", age: 30},{name: "Jane", age: 20},{name: "Jim", age: 25}];users.sort((a, b) => {if (a.age > b.age) return -1;if (a.age < b.age) return 1;return 0;}); console.log(users);// [ {name: "John", age: 30}, {name: "Jim", age: 25}, {name: "Jane", age: 20} ]

In this example, the comparison happens between a and b. If a comes after b in ascending order then it returns -1 which indicates that a should come before b in descending order. And similarly, if a comes before b in ascending order, it returns 1 that means that a should come after b in descending order.

If both are equal it will return 0 and that will maintained by their relative order.

You can simplify above compare function by using the ternary operators and can do it in one line:

users.sort((a, b) => a.age > b.age ? -1 : a.age < b.age ? 1 : 0);

It will show the same result as previous function.

Case-Insensitive Sorting

In many cases, we need to sort array of objects that are case-insensitive. eg, “aBc” comes before “XYZ” in ascending order by default.
and to do a case-insensitive sort, we need to convert the strings to lowercase before comparing them in the compare function.

For example:

interface Person {name: string;} let people: Person[] = [{name: "John"},{name: "paul"},{name: "George"},{name: "Ringo"}]; people.sort((a, b) => {let nameA = a.name.toLowerCase();let nameB = b.name.toLowerCase(); if (nameA < nameB) {return -1;}if (nameA > nameB) {return 1;} return 0;});

This converts name property of this object to lowercase before the comparison, so the end result is:

[ {name: "George"}, {name: "John"}, {name: "paul"}, {name: "Ringo"} ]

This approach works well for simple case-insensitive sorting of strings.
In case if you need to do more complex sorting, you can use a library like lodash which has built-in case-insensitive sorting functions.

Sorting Nested Objects

When sorting arrays of objects that contain nested objects, we need to access the nested properties in the compare callback function.

For example, if we have an array of objects with a nested address object:

const users = [{name: 'John',address: {city: 'New York'}},{name: 'Jane',address: {city: 'Los Angeles'}}];

We can sort it by accessing nested city property and then sort it like below:

users.sort((a, b) => {if(a.address.city < b.address.city) {return -1;}if(a.address.city > b.address.city) {return 1;}return 0;});

Inside the compare function, we can use a.address.city and b.address.city to access the nested city property and then compare the values. This process works for objects nested at any depth - we just chain the property accessors together to drill down into the nested structure. By accessing the nested properties in the compare callback, we can sort arrays of objects with any level of nesting.

Conclusion

In Typescript coding, it is very important to know sorting arrays and primitives like strings, numbers, and dates. But the most valuable thing is ability to sort arrays of objects by one or more properties. Sorting helps to organize complex data structures in a logical way and makes it easier to work within our code.

The Complete Guide to Sorting Arrays of Objects in Typescript (2024)

FAQs

The Complete Guide to Sorting Arrays of Objects in Typescript? ›

You can sort an array of objects by a single property like name or by age. In TypeScript the Array. sort() method accepts a compare function through which you can compare the name or age property of objects. In this example the compare function in sort method takes two Person objects a and b.

How do you sort an array with objects in TypeScript? ›

In TypeScript, you can use the Array. prototype. sort() method to sort an array of objects by a property value. The sort() method takes a comparator function as its argument, which should return a negative, zero, or positive value depending on the order in which the elements should be sorted.

How can you sort arrays of objects? ›

To sort an array of objects, use the sort() method with a compare function. A compareFunction applies rules to sort arrays by defined our own logic. They allow us to sort arrays of objects by strings, integers, dates, or any other custom property.

What is sort method in TS? ›

TypeScript Array sort() Method. The sort() method in TypeScript sorts the elements of an array and returns the sorted array. By default, it sorts an array in ascending order. It can take an optional compareFunction to define the sort order, allowing for custom sorting logic.

How to check array of objects in TypeScript? ›

In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method.

How do you sort an object in an array list? ›

An ArrayList can be sorted by using the sort() method of the Collections class in Java. It accepts an object of ArrayList as a parameter to be sort and returns an ArrayList sorted in the ascending order according to the natural ordering of its elements.

How do you search in an array of objects in TypeScript? ›

The find() method in TypeScript searches the first element in the array, that satisfies the conditions of the testing function. If no element in the array satisfies the condition, the method returns undefined.

How to sort an object of objects in JavaScript? ›

  1. function sortDataBy (data, byKey){ //here your logic code }
  2. let sortedData;
  3. if(byKey == 'name'){ //your logic for name } else if(byKey == 'age'){ //your logic for age }
  4. // we use javascript sort function to compare to value sortedData = data. ...
  5. sortedData = data. ...
  6. return sortedData;
  7. console.
Mar 25, 2021

Which sorting technique is best for arrays? ›

Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.

What is the fastest way to sort array? ›

In practice, Quick Sort is usually the fastest sorting algorithm. Its performance is measured most of the time in O(N × log N).

How do you sort values in TypeScript? ›

You can use the sort() method for sorting arrays of primitive values like strings, numbers, and booleans in TypeScript. In above the sort() method will sort the strings alphabetically in ascending order by default. You can similarly sort the numbers too. This will sort the numbers in ascending order as well.

What is an array in TypeScript? ›

In TypeScript, an array is a data structure that stores several values of the same type in a single variable. For instance, you can have an array of strings, numbers, or even objects. The array type is, in fact, a built-in data type in TypeScript, which makes it easy to declare and manipulate arrays.

What is the method to sort an array? ›

Using the sort() Method

In Java, Arrays is the class defined in the java.util package that provides sort() method to sort an array in ascending order. It uses Dual-Pivot Quicksort algorithm for sorting. Its complexity is O(n log(n)).

How to define an array of objects in TS? ›

TypeScript Arrays are a data type just like a string, Boolean, and number; we know there are many ways to declare the arrays in TypeScript. One is the Array of Objects in TypeScript; the user can define an array of objects by placing brackets after the interface. It can be named an interface or an inline interface.

How to compare an array of objects in TypeScript? ›

Methods to compare two arrays in Javascript are:
  1. Equality comparison: Using the == or === operators.
  2. JSON. stringify(): Converting the arrays to JSON strings, and then comparing those.
  3. For Loop: traversing both arrays using a for loop and comparing each element.
  4. Array. prototype. ...
  5. Array. prototype.
Jan 9, 2024

How to get an object from an array in TypeScript? ›

The Object. assign() method can be used to convert an array of objects into a single object by passing an empty object and using the rest parameter with the array to store its items into the empty object.

How do you filter an array of objects in TypeScript? ›

The 'filter()' Method in TypeScript

const newArray = array. filter(testFunction(element[, index[, array]])[, thisArg]); testFunction : The function used to test each element of the array. It should return true for elements that should be included in the new array.

How to sort an array of objects by string property? ›

Sort array of objects by string property value in JavaScript
  1. Approach 1: Using sort() with a custom compare function.
  2. Approach 2: sorting by a user-defined function.
  3. Approach 3: Using sort() with a comparison function and the toLowerCase() method.
  4. Approach 4: Using Intl.Collator for Locale-Aware String Comparison.
Jun 21, 2024

How do you sort an array of objects by time? ›

Sort an Object Array by Date in JavaScript
  1. Using sort method with Date objects.
  2. Using sort() method with getTime() method.
  3. Using a custom sorting function.
  4. Using Lodash _.orderBy() Method.
  5. Using Intl.DateTimeFormat and Array.prototype.sort()
May 31, 2024

How do you represent an array of objects in TypeScript? ›

Syntax: const array_name: Array<{key1: type1, key2: type2, key3: type3}> = [{}]; Example: This example will show you the use of the Array type to create an array of object in TypeScript.

References

Top Articles
Japanese Clear Soup Recipe
Christmas Red Velvet Sugar Cookies – Recipe from Yummiest Food Cookbook
James Earl Jones: George Lucas and Mark Hamill lead tributes to actor
Jennifer Riordan Net Worth: A Comprehensive Look At Her Life And Legacy
2016 Hyundai Sonata Refrigerant Capacity
Latina Webcam Lesbian
Elgin Il Building Department
M&T Bank Atm Locations Near Me
Santa Cruz Craigslist Cars And Trucks - By Owner
Triple the Potatoes: A Farmer's Guide to Bountiful Harvests
Stanley Steemer Medford Oregon
Pokemon Fire Red Download Pc
Swgoh Boba Fett Counter
1102 E Overland Trail Abilene 79601
What To Do With Mysterious Camera In Sakura Stand
Ruc Usmc List
Julie Green Ministries International On Rumble
KMST ver. 1.2.178 – Tallahart & the Long Awaited Balance Patch!
Aussiebigdaddik
Https //Myapps.microsoft.com Portal
Hahs Sentral
Redgifs.comn
Chatzy Spanking
Venus Nail Lounge Lake Elsinore
Biopark Prices
Filmy4Wap Xyz.com 2022
Freeway Insurance Actress
Mcdonald Hours Near Me
How To Get Coins In Path Of Titans
Linktree Teentinyangel
100000 Divided By 3
Us 25 Yard Sale Map
Arialectra Baby Alien
Magma Lozenge Location
Journal articles: 'State of New York and the Military Society of the War of 1812' – Grafiati
Deborah Clearbranch Psychologist Georgia
9 best hotels in Atlanta to check out in 2023 - The Points Guy
Bridger Elementary Logan
Strange World Showtimes Near Harkins Theatres Christown 14
Glowforge Forum
Urgent Care Pelham Nh
Acceltrax Sycamore Services
Katie Hamden Of
Zmeenaorrxclusive
Research Tome Neltharus
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Brokaw 24 Hour Fitness
Best Of Clinton Inc Used Cars
Tia V15.1 Update
Birmingham National Weather Service
Nfl Spotrac Transactions
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6608

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.