Warning: session_start(): open(/tmp/sess_1ceff616bbcd4fad6cb1783fbd90e377, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Sort An Array Of Strings In Descending Order In TypeScript - LearnShareIT

How To Sort An Array Of Strings In Descending Order In TypeScript

Sort an Array of Strings in Descending order in TypeScript

One of the most important skills that we should know when working with arrays is to sort an array of strings in descending order in TypeScript. If you are still struggling and don’t know how to do so, follow our instructions below for some of the best solutions.

Sort an array of strings in descending order in TypeScript

Using array.sort and array.reverse()

This is the most straightforward way to sort an array of strings. TypeScript has supported us with many built-in functions to work with arrays. And one of the most important ones is the array.sort() method. The array.sort() method will sort the array in an ascending order. 

Syntax

array.sort()

Return value: an array that has been sorted in an alphabet order.

For example:

var myArr = ['Bob', 'Tom', 'Alex', 'Zack', 'Johnson', 'Emily', 'Kate', 'Ivan', 'Alice'];

// Sort the array in alphabet order
var sortedArr = myArr.sort();     

console.log('This is the sorted array: ', sortedArr);

Output: 

This is the sorted array: ["Alex", "Alice", "Bob", "Emily", "Ivan", "Johnson", "Kate", "Tom", "Zack"] 

As you can see, our array has been sorted alphabetically and in an ascending order. If we want it to be in a descending order, we can use the array.reverse() method.

The array.reverse() method will reverse the order of the current array. 

Syntax

array.reverse()

Return value: a reversed array.

Example: 

var myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Reverse the current array
var result = myArr.reverse();

console.log(result);

Output

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Now, let’s try to apply it. 

Completed code: 

var myArr = ['Bob', 'Tom', 'Alex', 'Zack', 'Johnson', 'Emily', 'Kate', 'Ivan', 'Alice'];

// Sort the array in alphabet order
var sortedArr = myArr.sort();
console.log('This is the sorted ascending array: ', sortedArr);

// Sort the array in descending order
var descendingArr = sortedArr.reverse();
console.log('This is the sorted descending array: ',descendingArr);

Output:

This is the sorted ascending array: ["Alex", "Alice", "Bob", "Emily", "Ivan", "Johnson", "Kate", "Tom", "Zack"] 
This is the sorted descending array: ["Zack", "Tom", "Kate", "Johnson", "Ivan", "Emily", "Bob", "Alice", "Alex"] 

Using a for loop 

If you don’t want to use the array.reverse() method, there is still another way to sort the array descending. First, we still need to sort the array using the array.sort() method. Then, we will run a for loop, starting from the last element of the array back to the first element. And then using the array.push() method to add each element to a new array. That way, we will have a reverse array of the old sorted one.

Completed code: 

var myArr = ['Bob', 'Tom', 'Alex', 'Zack', 'Johnson', 'Emily', 'Kate', 'Ivan', 'Alice'];
var descendingArr = [];

// Sort the array in alphabet order
var sortedArr = myArr.sort();
console.log('This is the sorted ascending array: ', sortedArr);

// Sort the array in descending order
for (let i = sortedArr.length - 1; i>=0; i--) { 
    descendingArr.push(sortedArr[i]);
};

console.log('This is the sorted descending array: ', descendingArr);

Output:

This is the sorted ascending array: ["Alex", "Alice", "Bob", "Emily", "Ivan", "Johnson", "Kate", "Tom", "Zack"] 
This is the sorted descending array: ["Zack", "Tom", "Kate", "Johnson", "Ivan", "Emily", "Bob", "Alice", "Alex"] 

Summary

In this tutorial, we have shown you two different methods to sort an array of strings in descending order in TypeScript. The fastest way is to use the array.sort() and array.reverse() method. Let’s try it to get your desired results.

Maybe you are interested:

Leave a Reply

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