PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteUse a numeric comparator with sort(): numbers.sort((a, b) => b - a). This puts larger numbers before smaller ones, but changes the original array. If you need to keep the original order, use toSorted() instead, provided your runtime supports it.
Sort the array in place with sort()
For an array typed as number[], pass a comparator that subtracts the first value from the second in reverse order:
As an Amazon Associate I earn from qualifying purchases.
const numbers: number[] = [10, 3, 25, 7];
const descending = numbers.sort((a, b) => b - a);
console.log(descending); // [25, 10, 7, 3]
The comparator returns a negative value when its first argument should come before its second, a positive value when it should come after, and zero when they compare equal. With (a, b) => b - a, larger values are placed first. For ascending order, reverse the subtraction: (a, b) => a - b. See MDN’s Array.prototype.sort() reference.
Free tools Windows power users keep installed
One-click scans. No signup required.
sort() mutates the array and returns the same array reference, now sorted. Use it when changing the original array is intended:
#1 Best Overall
const scores: number[] = [12, 40, 7, 40];
scores.sort((a, b) => b - a);
console.log(scores); // [40, 40, 12, 7]
Keep the original array unchanged with toSorted()
toSorted() applies the same comparator but returns a new sorted array:
const scores: number[] = [12, 40, 7, 40];
const sortedScores = scores.toSorted((a, b) => b - a);
console.log(sortedScores); // [40, 40, 12, 7]
console.log(scores); // [12, 40, 7, 40]
MDN documents toSorted() as the non-mutating alternative. Check that the JavaScript runtime where your code runs supports it. If TypeScript does not recognize the method, check the project’s configured TypeScript lib target; changing type declarations alone does not add runtime support.
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
Why plain sort() does not sort numbers numerically
Without a comparator, JavaScript sorts elements by comparing their string conversions. That can produce an order that looks lexicographic rather than numeric: MDN gives [1, 30, 4, 21, 100000] as an example that sorts by default to [1, 100000, 21, 30, 4]. For numeric order, always provide a numeric comparator such as (a, b) => b - a.
Handle ties, invalid numbers, and string input
Equal values
For finite numbers, equal values make the subtraction comparator return zero. JavaScript’s array sort is stable, so elements that compare equal retain their relative order.
NaN values
If an array may contain NaN, subtraction can itself return NaN, so the simple comparator may not express the ordering you want. Decide where NaN belongs and handle it explicitly in the comparator rather than assuming ordinary numeric subtraction covers it.
Values arriving as strings
These examples assume the array contains numbers. If values arrive as strings, validate and convert them before sorting; a comparator intended for numeric values does not guarantee the intended result for unvalidated input.
Keep the comparator consistent
A comparator should be pure and give consistent results for repeated pairs; its ordering should also be reflexive, antisymmetric, and transitive. These rules help the sort produce a predictable order.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsTypeScript uses JavaScript’s sorting behavior
There is no TypeScript-specific sorting API or package needed for this task. TypeScript preserves JavaScript’s runtime behavior, so the same array methods and comparator work in TypeScript as in JavaScript. The relevant distinction is whether you want to mutate the existing array with sort() or create a sorted copy with toSorted().
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




