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.
Comments
Post a Comment
Leave Comment