How to sort arrays numerically by object property value with JavaScript?

To sort arrays numerically by object property value with JavaScript, we use the sort method.

For instance, we write

const sorted = myArray.sort((a, b) => a.distance - b.distance);

to call myArray.sort with a callback that sorts by the distance property of each object in myArray in ascending order.