Skip to content

Latest commit

 

History

History
19 lines (14 loc) · 343 Bytes

File metadata and controls

19 lines (14 loc) · 343 Bytes

sortByKey()

Overview

Sorts an array of objects based on the values of a specified key.

Code

A screenshot of the titular code snippet

const sortByKey = (array, key) => {
  array.sort((a, b) => {
    const x = a[key];
    const y = b[key];
    return x < y ? -1 : x > y ? 1 : 0;
  });
};