Skip to content

2724. Sort By 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };
type Fn = (value: JSONValue) => number;

function sortBy(arr: JSONValue[], fn: Fn): JSONValue[] {
  arr.sort((a, b) => fn(a) - fn(b));
  return arr;
}