API Reference

Sorts an array of objects by a specific shared key's value.

function sortByKeyValue<T extends object, U extends keyof T>(
  arr: T[],
  key: U,
  order: "asc" | "desc" = "asc"
)

// Example:

const obj1 = { a: 3, b: 2 }
const obj2 = { a: 1, b: 2 }
const obj3 = { a: 2, b: 2 }
 
sortByKeyValue([obj1, obj2, obj3], "a")
//=>
     [
       { a: 1, b: 2 },
       { a: 2, b: 2 },
       { a: 3, b: 2 },
     ]