If you need the biggest numbers first in a TypeScript array, sort the array in descending order using a compare function: sales.sort((a, b) => b - a). Without it, sort() compares numbers as text, and 15000 lands before 2300.
In this tutorial, you’ll learn to sort a number array in descending order using TypeScript, keep the original, and sort objects with a localeCompare() tie-break.
Every output below is a real run: TypeScript 7.0.2 and Node.js v22.22.2, strict mode, VS Code.
Why Doesn’t sort() Order Numbers Correctly in TypeScript?
Every example uses six monthly sales totals from a Denver store, with mixed digit counts:
const sales: number[] = [1250, 980, 15000, 640, 2300, 75];
console.log("sort(): ", [...sales].sort());
console.log("sort().reverse():", [...sales].sort().reverse());
Output:
sort(): [ 1250, 15000, 2300, 640, 75, 980 ]
sort().reverse(): [ 980, 75, 640, 2300, 15000, 1250 ]

sort() puts 15000 after 1250; reversing keeps the mistake.How does this code work?
With no compare function, JavaScript compares numbers as strings. “15000” starts with “1”, so it beats “2300”.
TypeScript doesn’t warn you, because the call is valid. The basics are in my TypeScript array sorting tutorial.
Method 1: Sort Descending with sort((a, b) => b – a) (Recommended)
Use this when you won’t need the original order. Same sales, now with a compare function:
const sales: number[] = [1250, 980, 15000, 640, 2300, 75];
const result = sales.sort((a, b) => b - a);
console.log("sorted: ", result);
console.log("same array? ", result === sales);
Output:
sorted: [ 15000, 2300, 1250, 980, 640, 75 ]
same array? true

The array now reads 15000, 2300, 1250, 980, 640, 75.
How does this code work?
A positive return value moves b ahead of a, so b - a puts the larger number first. Use a - b for ascending.
Important: sort() changes the array in place (same array? is true), and reverse() never sorts (reversing an array in TypeScript).
Method 2: Keep the Original Array with toSorted() in TypeScript
Need the unsorted data later? Sort a copy. Same sales, two ways:
const sales: number[] = [1250, 980, 15000, 640, 2300, 75];
const desc = sales.toSorted((a, b) => b - a); // ES2023+
const older = [...sales].sort((a, b) => b - a); // any target
console.log("toSorted:", desc);
console.log("spread: ", older);
console.log("original:", sales);
Output:
toSorted: [ 15000, 2300, 1250, 980, 640, 75 ]
spread: [ 15000, 2300, 1250, 980, 640, 75 ]
original: [ 1250, 980, 15000, 640, 2300, 75 ]

original keeps its order.How does this code work?
toSorted() returns a new array. Older targets? Copy with the TypeScript spread operator, then sort().
It needs ES2023. With lib at es2022, my TypeScript build failed with TS2550, so raise lib in tsconfig.json or use the spread.
Method 3: Sort an Array of Objects by a Number, Descending
Four store products by price, high to low, names A to Z on a tie:
const products = [
{ name: "Office Chair", price: 249 },
{ name: "Monitor Arm", price: 89.5 },
{ name: "Bookshelf", price: 89.5 },
{ name: "USB Hub", price: 24.99 },
];
const byPrice = products.toSorted(
(a, b) => b.price - a.price || a.name.localeCompare(b.name)
);
for (const p of byPrice) {
console.log(`${p.name.padEnd(13)} $${p.price.toFixed(2)}`);
}
Output:
Office Chair $249.00
Bookshelf $89.50
Monitor Arm $89.50
USB Hub $24.99

Office Chair leads at $249.00, Bookshelf beats Monitor Arm at $89.50, and USB Hub’s last.
How does this code work?
A price tie returns 0, so || falls through to localeCompare(). For a localeCompare descending sort, swap sides: b.name.localeCompare(a.name). See comparing strings in TypeScript.
Dates or several keys? See sorting objects by property value.
Which TypeScript Descending Sort Should You Pick?
| Approach | Use it when | Original |
|---|---|---|
sort((a, b) => b - a) | You own the array | Changed |
toSorted(...) | ES2023+, need a copy | Kept |
[...arr].sort(...) | Older targets | Kept |
Descending Sort Gotchas in TypeScript
In TypeScript, CSV data arrives as strings. Same sales, with one N/A:
const raw = ["1250", "980", "N/A", "15000", "75"];
const nums = raw.map(Number);
console.log("with NaN:", nums.toSorted((a, b) => b - a));
const clean = nums.filter((n) => !Number.isNaN(n));
console.log("cleaned: ", clean.toSorted((a, b) => b - a));
Output:
with NaN: [ 1250, 980, NaN, 15000, 75 ]
cleaned: [ 15000, 1250, 980, 75 ]

NaN froze the order; filtering fixed it.NaNmakesb - areturnNaN, read as a tie. Filter first.- Convert strings first; see converting a string to a number in TypeScript.
sort()mutates, so usetoSorted()on React state.
More TypeScript sorting and array posts worth a look:
- Sort an array alphabetically in TypeScript
- Sort an array by date in TypeScript
- Sort by enum in TypeScript
- Sum an array in TypeScript
Questions About Sorting Descending in TypeScript
How do I sort numbers in descending order in TypeScript?
Call arr.sort((a, b) => b - a).
Why does sort() put 15000 before 2300?
It compares them as strings, and “1” sorts before “2”.
Does sort() change the original array?
Yes. toSorted() and [...arr].sort() return a copy.
How do I use localeCompare for descending order?
Put b first: b.name.localeCompare(a.name).
Is sort().reverse() a descending sort?
Only after a numeric sort: sort((a, b) => a - b).reverse().
Where is Array.prototype.toSorted() documented?
See Array.prototype.toSorted() on MDN.
