koehr.ing/content/til/2024-05-12.smd
2024-09-16 22:02:39 +02:00

21 lines
478 B
Text

---
{
.title = "Sort list of strings in Javascript",
.date = @date("2024-05-12T00:00:00"),
.author = "koehr",
.draft = false,
.layout = "til.shtml",
.description = "",
.tags = [],
.custom = { .source = "https://stackoverflow.com/questions/6712034/sort-array-by-firstname-alphabetically-in-javascript" },
}
---
```ts
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
```
or reversed order:
```ts
users.sort((a, b) => a.firstname.localeCompare(b.firstname) * -1)
```