How to Sort a Number Array in Descending Order using TypeScript?

Recently, one of my team members asked me to sort a numeric array in descending order. In this tutorial, I will explain how to sort a number array in descending order using TypeScript, using different methods and examples.

Understanding the sort() Method

TypeScript, being a superset of JavaScript, inherits the sort() method for arrays. By default, the sort() method sorts elements in ascending order. However, we can customize the sorting behavior by providing a comparison function.

Here’s a simple example of sorting an array of numbers in ascending order:

const numbers: number[] = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
numbers.sort();
console.log(numbers);
// Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

I executed the above TypeScript code and you can see the exact output in the screenshot below:

Sort a Number Array in Descending Order using TypeScript

Check out Sort an Array of Objects by Property in TypeScript

Sorting Numbers in Descending Order

To sort a number array in descending order, we need to provide a custom comparison function to the sort() method. The comparison function takes two parameters, a and b, and returns a negative value if a should be sorted before b, a positive value if b should be sorted before a, or zero if they are considered equal.

Here’s an example of sorting numbers in descending order:

const americanCities: number[] = [1776, 1492, 1620, 1865, 1945, 2001];
americanCities.sort((a, b) => b - a);
console.log(americanCities);
// Output: [2001, 1945, 1865, 1776, 1620, 1492]

In this example, we have an array americanCities containing notable years in American history. By providing the comparison function (a, b) => b - a, we sort the array in descending order.

The comparison function subtracts a from b, resulting in a positive value when b is greater than a, effectively sorting the array in descending order.

Here is the exact output in the screenshot below:

Sorting Numbers in Descending Order in TypeScript

Read Search an Array of Objects by Property in TypeScript

Sorting an Array of Objects

In real-world scenarios, we often deal with arrays of objects. Let’s consider an example where we have an array of objects representing U.S. presidents, and we want to sort them based on their birth year in descending order.

interface President {
  name: string;
  birthYear: number;
}

const usPresidents: President[] = [
  { name: "Joe Biden", birthYear: 1942 },
  { name: "Donald Trump", birthYear: 1946 },
  { name: "Barack Obama", birthYear: 1961 },
  { name: "George W. Bush", birthYear: 1946 },
  { name: "Bill Clinton", birthYear: 1946 },
];

usPresidents.sort((a, b) => b.birthYear - a.birthYear);
console.log(usPresidents);

Output:

[
  { name: 'Barack Obama', birthYear: 1961 },
  { name: 'Donald Trump', birthYear: 1946 },
  { name: 'George W. Bush', birthYear: 1946 },
  { name: 'Bill Clinton', birthYear: 1946 },
  { name: 'Joe Biden', birthYear: 1942 }
]

In this example, we define an interface President to represent the structure of the objects in the usPresidents array. Each object has a name and a birthYear property. We use the sort() method with a comparison function that compares the birthYear property of the objects, sorting them in descending order.

Check out Remove Undefined Values from an Array in TypeScript

Sorting Arrays with Custom Comparison Functions

Sometimes, we may need more complex sorting logic based on multiple properties or conditional statements. We can achieve this by writing custom comparison functions.

Let’s consider an example where we have an array of U.S. states, and we want to sort them based on their population in descending order. However, if two states have the same population, we want to sort them alphabetically.

interface State {
  name: string;
  population: number;
}

const usStates: State[] = [
  { name: "California", population: 39512223 },
  { name: "Texas", population: 28995881 },
  { name: "Florida", population: 21477737 },
  { name: "New York", population: 19453561 },
  { name: "Pennsylvania", population: 12801989 },
  { name: "Illinois", population: 12671821 },
  { name: "Ohio", population: 11689100 },
  { name: "Georgia", population: 10617423 },
  { name: "North Carolina", population: 10488084 },
  { name: "Michigan", population: 9986857 },
];

usStates.sort((a, b) => {
  if (b.population === a.population) {
    return a.name.localeCompare(b.name);
  }
  return b.population - a.population;
});

console.log(usStates);

Output:

[
  { name: 'California', population: 39512223 },
  { name: 'Texas', population: 28995881 },
  { name: 'Florida', population: 21477737 },
  { name: 'New York', population: 19453561 },
  { name: 'Illinois', population: 12671821 },
  { name: 'Pennsylvania', population: 12801989 },
  { name: 'Georgia', population: 10617423 },
  { name: 'Michigan', population: 9986857 },
  { name: 'North Carolina', population: 10488084 },
  { name: 'Ohio', population: 11689100 }
]

In this example, we define an interface State to represent the structure of the objects in the usStates array. Each object has a name and a population property.

The custom comparison function first checks if the populations of two states are equal. If they are, it uses the localeCompare() method to compare the names of the states alphabetically. If the populations are different, it sorts them in descending order based on the population.

Conclusion

Sorting number arrays in descending order using TypeScript is easy using the built-in sort() method. By providing a custom comparison function, we can easily sort numbers and objects and implement complex sorting logic based on multiple properties or conditions.

I hope this tutorial has given you a clear understanding of how to sort number arrays in descending order using TypeScript.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.