TypeScript Sorting Array - GeeksforGeeks (2024)

Last Updated : 02 Jan, 2024



Like Article




To sort an array in TypeScript we could use Array.sort() function. TheArray.sort()is an inbuilt TypeScript function that is used to sort the elements of an array.



Below are the places where we could use the sort() function:

Table of Content

  • Sorting array of numbers
  • Sorting Array of Strings
  • Sorting Array of Objects

Sorting Array of Numbers

Example: In this example, we have an array of numbers, and we use the sort method with a custom comparison function to sort the array in ascending order.


Sorted Numbers: [1, 2, 4, 5, 7, 9]

Sorting Array of Strings

Example: In this example, we have an array of strings, and we use the sort method to sort the strings in default lexicographic order.


const fruits: string[] = ["Apple", "Orange", "Banana", "Mango"];


console.log("Sorted Fruits: ", fruits);


Sorted Fruits: [ 'Apple', 'Banana', 'Mango', 'Orange' ]

Sorting Array of Objects

Example: In this example, we have an array of people with name and age properties. We use the sort method with a custom comparison function to sort the array based on the age property in ascending order.


interface Person {

name: string;

age: number;


const people: Person[] = [

{ name: "Alice", age: 25 },

{ name: "Bob", age: 30 },

{ name: "Charlie", age: 22 },

{ name: "David", age: 28 }


people.sort((a, b) => a.age - b.age);

console.log("Sorted People by Age: ", people);


Sorted People by Age: [ 
{ name: 'Charlie', age: 22 },
{ name: 'Alice', age: 25 },
{ name: 'David', age: 28 },
{ name: 'Bob', age: 30 }




Next Article

TypeScript Arrays

Please Login to comment...

Similar Reads

How to move Null Values at the end Regardless of the Sorting Direction in TypeScript ? A few times, we face situations where we need to sort values with null values in them. In this, we sort it and move the null values to the end regardless of the sorting direction. In TypeScript, achieving this requires special handling and sowe are going to discuss those processes. These are the following ways: Table of Content Using custom Sorting 2 min read How to Make a Generic Sorting Function in TypeScript ? Sorting is a fundamental operation in programming, allowing us to organize data in a specific order for easier manipulation and retrieval. In TypeScript, creating a generic sorting function provides flexibility and reusability across different data types. Table of Content Using Array.prototype.sort() methodBy creating custom sorting logicUsing Arra 4 min read Difference between sorting string array and numerical array The array.sort() method sorts the string array in alphabetical order. This method sorts string arrays based on Unicode(). Unicode provides a unique number for every character, no matter what platform, device, application, or language. This allows every modern system to use Unicode. These should be started with uppercase and lowercase letters. Synta 3 min read How to sorting an array without using loops in Node.js ? The setInterval() method repeats or re-schedules the given function at every given time-interval. It is somewhat like window.setInterval() Method of JavaScript API, however, a string of code can't be passed to get it executed. Syntax: setInterval(timerFunction, millisecondsTime); Parameter: It accepts two parameters which are mentioned above and de 2 min read Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2} Recommended: Please sol 5 min read Sorting an Array of Binary Values using JavaScript JavaScript allows us to sort an array containing only binary values i.e. 0 and 1, below is an example to understand the problem. Example: Input array = [ 0 , 1, 0, 0 , 0, 1, 0 , 1, 0 , 1, 1]Output array = [ 0 , 0 , 0 , 0 , 0, 0 , 1 , 1, 1, 1]Below are several approaches to sorting an array of binary values using JavaScript which are as follows: Tab 2 min read Sorting Array of Exactly Three Unique Repeating Elements in JavaScript Given an array of Numbers that contain any frequency of exactly three unique elements. Our task is to sort this array using JavaScript. Examples: Input: arr[] = [3, 1, 3, 2, 1, 2, 1, 3, 2]Output: [1, 1, 1, 2, 2, 2,3,3,3]Input = arr[] =[1,2,3,1,2,3]Output: [1,1,2,2,3,3]Table of Content Counting Sort with Extra SpaceThree-Way PartitioningCounting Sor 2 min read Count Unique Elements in Array Without Sorting using JavaScript One can count unique elements(distinct elements) present in an array without sorting JavaScript. There are several methods of counting unique elements without sorting in JavaScript. Below is an example to understand the problem clearly. Example:Input: [ 1,2, 3, 1, 3, 4, 5, 5, 2] Output: 5 Explanation: Unique Elements in array : 1, 2, 3, 4, 5. Total 3 min read Sorting Array of Number by Increasing Frequency using JavaScript To sort an array of given numbers based on the frequency of occurrence of each number first sort the array such that the elements repeated for the least number of times appear first followed by the elements with increasing frequency. Example: Input:array = [3, 3, 1, 1, 1, 8, 3, 6, 8, 8] Output:[6, 1, 1, 1, 3, 3, 3, 8, 8, 8]Explanation:Number 6 occu 3 min read Alternative Sorting of an Array using JavaScript Alternative sorting means we have to arrange the elements of an array in such a way that the first element is the first maximum and the second element is the first minimum, the third element is the second maximum, the fourth element is the second minimum, and so on. Example: Input: array = [5, 6, 2, 4, 3, 1]Output: array = [6, 1, 5, 2, 4, 3] Explan 3 min read

Article Tags :

  • JavaScript
  • TypeScript
  • Web Technologies
TypeScript Sorting Array - GeeksforGeeks (2024)


Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6094

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.