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.

Javascript

const numbers: number[] = [4, 2, 7, 1, 9, 5];

numbers.sort((a, b) => a - b);

console.log("Sorted Numbers: ", numbers);

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)

References

Top Articles
Best R-Rated Comedies of All Time, Ranked
AP® World History Score Calculator | Albert Resources
Tiny Tina Deadshot Build
How To Fix Epson Printer Error Code 0x9e
Research Tome Neltharus
Winston Salem Nc Craigslist
Unity Stuck Reload Script Assemblies
The 10 Best Restaurants In Freiburg Germany
How Many Cc's Is A 96 Cubic Inch Engine
Flixtor The Meg
50 Meowbahh Fun Facts: Net Worth, Age, Birthday, Face Reveal, YouTube Earnings, Girlfriend, Doxxed, Discord, Fanart, TikTok, Instagram, Etc
360 Training Alcohol Final Exam Answers
Mcoc Immunity Chart July 2022
Tap Tap Run Coupon Codes
CHESAPEAKE WV :: Topix, Craigslist Replacement
123 Movies Black Adam
Savage X Fenty Wiki
Best Restaurants Ventnor
Washington, D.C. - Capital, Founding, Monumental
U/Apprenhensive_You8924
Fairy Liquid Near Me
Moparts Com Forum
Craiglist Galveston
How to find cash from balance sheet?
Jayah And Kimora Phone Number
Delaware Skip The Games
PowerXL Smokeless Grill- Elektrische Grill - Rookloos & geurloos grillplezier - met... | bol
Kashchey Vodka
Reptile Expo Fayetteville Nc
Lakewood Campground Golf Cart Rental
Nearest Ups Ground Drop Off
Filmy Met
Taktube Irani
Craigslist Cars And Trucks Mcallen
Khatrimmaza
Play 1v1 LOL 66 EZ → UNBLOCKED on 66games.io
Tds Wifi Outage
The Banshees Of Inisherin Showtimes Near Reading Cinemas Town Square
All Characters in Omega Strikers
Energy Management and Control System Expert (f/m/d) for Battery Storage Systems | StudySmarter - Talents
30 Years Of Adonis Eng Sub
'The Nun II' Ending Explained: Does the Immortal Valak Die This Time?
Flappy Bird Cool Math Games
Gli italiani buttano sempre più cibo, quasi 7 etti a settimana (a testa)
Noga Funeral Home Obituaries
Bridgeport Police Blotter Today
Mit diesen geheimen Codes verständigen sich Crew-Mitglieder
Model Center Jasmin
Optimal Perks Rs3
Costco Gas Price Fort Lauderdale
Cognitive Function Test Potomac Falls
Pauline Frommer's Paris 2007 (Pauline Frommer Guides) - SILO.PUB
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6845

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.