How To Sort an Array in TypeScript

To sort the elements in an array, we will use the built-in sort() method in TypeScript. This article will show you how to sort based on different types of arrays.

Ways to sort an array in TypeScript

TypeScript has a built-in method to easily sort the elements in an array. The usage depends on the data type of that array. This method modifies the original array and returns a sorted array. The syntax of this method is as follows.

Syntax:

array.sort();
array.sort((itemA, itemB) => itemA - itemB);

Parameter:

This method can take a function to compare values with each other for sorting purposes

Return Value:

This method returns a sorted array.

Sorting a number array

To sort an array of numbers, we will pass a comparison function. Depending on the comparison, we can sort in ascending or descending order.

Example:

const numberArray: number[] = [8, 1, 5, 3, 6];

// Pass in a function to sort in ascending fashion
const sortedArray = numberArray.sort((itemA, itemB) => itemA - itemB);
console.log(sortedArray);

Output:

[1, 3, 5, 6, 8]

With a slight change in the comparison function, we can sort in descending fashion.

Example:

const numberArray: number[] = [8, 1, 5, 3, 6];

// Change position of itemA and itemB to sort descending
const sortedArray = numberArray.sort((itemA, itemB) => itemB - itemA);
console.log(sortedArray);

Output:

[8, 6, 5, 3, 1]

Sorting a string array

For a string array, we may not need to pass in the comparison function

Example:

const nameArray: string[] = ["John", "Peter", "Mary", "Yennefer", "Jax"];

// We may not need to pass in the comparison function
const sortedArray = nameArray.sort();
console.log(sortedArray);

Output:

["Jax", "John", "Mary", "Peter", "Yennefer"]

Sorting an object array

To be able to sort an array of objects, in the comparison function, we will return a value greater than 0 or less than 0.

Example:

// Create an unsorted array of objects
const objectArray: { price: number }[] = [
    { price: 99 },
    { price: 399 },
    { price: 299 },
    { price: 199 },
];

// Compares elements and returns a positive or negative value
const sortedArray = objectArray.sort((itemA, itemB) => {
    if (itemA.price > itemB.price) {
        return 1;
    }

    if (itemA.price < itemB.price) {
        return -1;
    }

    return 0;
});

console.log(sortedArray);

Output:

[ { price: 99 }, { price: 199 }, { price: 299 }, { price: 399 } ]

In the above example, we will use conditional expressions in the comparison function if itemA is greater than itemB then we return 1, so the function will be sorted in ascending fashion.

Summary

In conclusion, to sort an array in TypeScript, we will use the built-in sort() method in TypeScript. Depending on the different types of arrays, we have different ways of using the comparison function. That’s the most effective way, and you don’t need any other methods.

Leave a Reply

Your email address will not be published. Required fields are marked *