TypeScript Sorting Array Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report To sort an array in TypeScript we could use Array.sort() function. The Array.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 numbersSorting Array of Strings Sorting Array of ObjectsSorting Array of NumbersExample: 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 ObjectsExample: 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 } ] Comment More infoAdvertise with us Next Article How to Sort Objects in an Array Based on a Property in a Specific Order in TypeScript ? A amanv09 Follow Improve Article Tags : TypeScript Similar Reads 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.Syntaxarray.sort( compareFunction )Parameter: This method 2 min read How to Sort an Array in TypeScript ? Array sorting is the process of arranging the elements within an array in a specified order, often either ascending or descending based on predetermined criteria. Below are the approaches used to sort an array in typescript: Table of Content Method 1: Using sort methodMethod 2: Spread OperatorMethod 3 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( 4 min read How to Sort an Array of Object using a Value in TypeScript ? Sorting an array of objects using a value in TypeScript pertains to the process of arranging an array of objects in a specified order based on a particular property or value contained within each object. The below approaches can be used to sort an array of objects using a value in TypeScript: Table 3 min read How to Sort Objects in an Array Based on a Property in a Specific Order in TypeScript ? Sorting objects in an array based on a specific property is a common task in software development. TypeScript, with its static typing and powerful features, provides various approaches to accomplish this task efficiently. The below approaches can be used to sort an array of objects in properties. Ta 3 min read Program for sorting variables of any data type Given an array of any data type, the task is to sort the given array without using in-built sorting functions.Examples:Input: arr[] = [64, 34, 25, 12, 22, 11, 90]Output: 11 12 22 25 34 64 90 Input: arr[] = ["banana", "apple", "orange", "grape", "kiwi"]Output: apple banana grape kiwi orange Input: ar 7 min read Like