Use JavaScript’s Array.prototype.sort() with a comparator that reads the property you want to order by. For numbers, subtract the values; for strings, compare them with localeCompare(). Note that sort() changes the original array.
Sort by a numeric property
For ascending order, return the difference between the corresponding properties:
As an Amazon Associate I earn from qualifying purchases.
type Item = { name: string; score: number };
const items: Item[] = [
{ name: "Ada", score: 9 },
{ name: "Lin", score: 4 },
];
items.sort((a, b) => a.score - b.score);
The comparator’s sign determines the order: a negative result puts a before b, a positive result puts it after, and zero treats them as equal. For descending order, reverse the operands:
items.sort((a, b) => b.score - a.score);
This subtraction pattern is appropriate for valid numeric values. If a property might be NaN or missing, define how those cases should be ordered rather than relying on subtraction.
#1 Best Overall
Sort by a string property
Use localeCompare() to compare string fields:
items.sort((a, b) => a.name.localeCompare(b.name));
For user-facing text, choose locale-aware behavior to suit the application’s language and data; there is no single locale policy that fits every use case.
Choose whether to change the original array
sort() rearranges the array in place and returns the same array reference. If you need to keep the original ordering, use toSorted() where both the runtime and TypeScript library definitions support it:
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
const sorted = items.toSorted((a, b) => a.score - b.score);
For broader compatibility, copy the array before sorting:
const sorted = [...items].sort((a, b) => a.score - b.score);
This is a shallow copy: the new array has a different ordering, but its elements are references to the same objects.
Common pitfalls and edge cases
- Do not rely on the default order for numbers. Without a comparator,
sort()converts values to strings, so numeric values can be ordered lexically rather than numerically. - Handle optional properties deliberately. If a key has a type such as
string | undefined, decide where missing values belong before writing the comparison. A generic helper should constrain its selected key to comparable values and account for such unions. - Keep the comparator consistent. Return zero when the selected values compare equal. Modern
sort()is stable, so equal elements retain their relative order. A comparator that returns only zero and one sign does not express a valid, consistent ordering. - Avoid side effects. The comparator should give consistent results and should not mutate the objects it compares.
Choose the comparator for your data
| Property and goal | Comparator | Notes |
|---|---|---|
| Valid numbers, ascending | (a, b) => a.value - b.value |
Pass a comparator to get numeric rather than default string-based ordering. |
| Valid numbers, descending | (a, b) => b.value - a.value |
Reverse the operands. |
| Strings | (a, b) => a.name.localeCompare(b.name) |
For human-readable text, choose the locale behavior deliberately. |
| Keep the input unchanged | toSorted() or [...items].sort() |
toSorted() requires runtime and TypeScript library support; copying first is an alternative. |
See MDN’s Array.prototype.sort reference for the method’s comparator behavior and additional examples.
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.




