🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will explore the sort() method for arrays in TypeScript. This method is a built-in function that sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings and comparing their sequences of UTF-16 code unit values. Understanding how to use sort() is useful for organizing and managing arrays.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, but you can provide a compare function to define a custom sort order.
2. Syntax
array.sort(compareFunction?);
Parameters
compareFunction(optional): A function that defines the sort order. If omitted, the array elements are converted to strings and sorted according to each character's Unicode code point value.
Return Value
The method returns the sorted array. The array is sorted in place, and no copy is made.
3. Examples
Let's look at some examples to understand how sort() works in TypeScript.
Example 1: Basic Usage (Default Sorting)
In this example, we use sort() to sort an array of numbers in ascending order.
let numbers: number[] = [4, 2, 5, 1, 3];
let sortedNumbers = numbers.sort();
console.log(sortedNumbers); // Output: [1, 2, 3, 4, 5]
Example 2: Sorting Strings
In this example, we use sort() to sort an array of strings in alphabetical order.
let fruits: string[] = ["banana", "apple", "mango"];
let sortedFruits = fruits.sort();
console.log(sortedFruits); // Output: ["apple", "banana", "mango"]
Example 3: Custom Sorting (Numeric Order)
In this example, we use sort() with a compare function to sort an array of numbers in ascending numeric order.
let numbers: number[] = [10, 2, 30, 4];
let sortedNumbers = numbers.sort((a, b) => a - b);
console.log(sortedNumbers); // Output: [2, 4, 10, 30]
Example 4: Custom Sorting (Descending Order)
In this example, we use sort() with a compare function to sort an array of numbers in descending order.
let numbers: number[] = [10, 2, 30, 4];
let sortedNumbers = numbers.sort((a, b) => b - a);
console.log(sortedNumbers); // Output: [30, 10, 4, 2]
Example 5: Sorting an Array of Objects
In this example, we use sort() to sort an array of objects based on a property value.
interface Person {
name: string;
age: number;
}
let people: Person[] = [
{ name: "Ravi", age: 25 },
{ name: "Ankit", age: 30 },
{ name: "Priya", age: 28 }
];
let sortedPeople = people.sort((a, b) => a.age - b.age);
console.log(sortedPeople);
// Output:
// [ { name: 'Ravi', age: 25 },
// { name: 'Priya', age: 28 },
// { name: 'Ankit', age: 30 } ]
Example 6: Sorting with Mixed Data Types
In this example, we use sort() to sort an array with mixed data types.
let mixedArray: (number | string)[] = [10, "apple", 2, "banana"];
let sortedMixedArray = mixedArray.sort();
console.log(sortedMixedArray); // Output: [10, 2, "apple", "banana"]
Example 7: Sorting with Negative Numbers
In this example, we use sort() to sort an array of negative and positive numbers.
let numbers: number[] = [-10, -30, 20, 5];
let sortedNumbers = numbers.sort((a, b) => a - b);
console.log(sortedNumbers); // Output: [-30, -10, 5, 20]
4. Conclusion
In this chapter, we explored the sort() method for arrays in TypeScript, which is used to sort the elements of an array in place. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use sort() effectively can help in various array manipulation tasks in TypeScript, especially when organizing and managing arrays.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment