TypeScript Sorting Array - GeeksforGeeks (2024)

Last Updated : 02 Jan, 2024

Improve

Improve

Like Article

Like

Save

Report

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.

Syntax:

array.sort(compareFunction)

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.

Output:

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.

Javascript

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

fruits.sort();

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

Output:

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.

Javascript

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);

Output:

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


A

amanv09

Improve

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 Php 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 4 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

Article Tags :

  • JavaScript
  • TypeScript
  • Web Technologies
TypeScript Sorting Array - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 6330

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.