Deviopath › JavaScript

Sort by Multiple Keys

Hard · Arrays

Write a function sortBy(items, keys) that returns a new sorted array of objects. keys is a list like ["-age", "name"].

- Compare by the first key; on a tie, by the next key, and so on. A - prefix means descending.

- A key can be a dotted path like "info.city".

- Numbers compare by value. Strings compare case-insensitively and "naturally": runs of digits compare as numbers, so "file2" comes before "file10".

- Missing and null values always go last, in both directions.

- Items that tie on every key keep their original order.

Values of one key are all numbers or all strings (letters and digits only).

Next: Recurring Dates