{"version":3,"file":"get-ordered-by.mjs","names":[],"sources":["../src/get-ordered-by.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       🗲 Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\ntype Order = \"asc\" | \"desc\";\n\n/**\n * Sorts an array of objects based on multiple properties and their corresponding order directions.\n *\n * @remarks\n * This function takes an array of objects, an array of keys to sort by, and an array of order directions.\n * It returns the sorted array, ordering by each key according to its corresponding direction\n * ('asc' for ascending or 'desc' for descending). If values for a key are equal,\n * it moves to the next key to determine the order.\n *\n * @example\n * ```typescript\n * // Sort an array of objects by 'user' in ascending order and 'age' in descending order.\n * const users = [\n *   { user: 'fred', age: 48 },\n *   { user: 'barney', age: 34 },\n *   { user: 'fred', age: 40 },\n *   { user: 'barney', age: 36 },\n * ];\n * const result = orderBy(users, ['user', 'age'], ['asc', 'desc']);\n * // result will be:\n * // [\n * //   { user: 'barney', age: 36 },\n * //   { user: 'barney', age: 34 },\n * //   { user: 'fred', age: 48 },\n * //   { user: 'fred', age: 40 },\n * // ]\n * ```\n *\n * @param collection - The array of objects to be sorted.\n * @param keys - An array of keys (properties) by which to sort.\n * @param orders - An array of order directions ('asc' for ascending or 'desc' for descending).\n * @returns The sorted array.\n */\nexport function getOrderedBy<T>(\n  collection: T[],\n  keys: (keyof T)[],\n  orders: Order[]\n): T[] {\n  const compareValues = (a: T[keyof T], b: T[keyof T], order: Order) => {\n    if (a < b) {\n      return order === \"asc\" ? -1 : 1;\n    }\n    if (a > b) {\n      return order === \"asc\" ? 1 : -1;\n    }\n    return 0;\n  };\n\n  const effectiveOrders = keys.map(\n    (_, index) => orders[index] ?? orders.at(-1)\n  );\n\n  return [...collection].sort((a, b) => {\n    for (const [i, key] of keys.entries()) {\n      const order = effectiveOrders[i];\n      const result = compareValues(a[key], b[key], order!);\n      if (result !== 0) {\n        return result;\n      }\n    }\n    return 0;\n  });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDA,SAAgB,aACd,YACA,MACA,QACK;CACL,MAAM,iBAAiB,GAAe,GAAe,UAAiB;AACpE,MAAI,IAAI,EACN,QAAO,UAAU,QAAQ,KAAK;AAEhC,MAAI,IAAI,EACN,QAAO,UAAU,QAAQ,IAAI;AAE/B,SAAO;;CAGT,MAAM,kBAAkB,KAAK,KAC1B,GAAG,UAAU,OAAO,UAAU,OAAO,GAAG,GAAG,CAC7C;AAED,QAAO,CAAC,GAAG,WAAW,CAAC,MAAM,GAAG,MAAM;AACpC,OAAK,MAAM,CAAC,GAAG,QAAQ,KAAK,SAAS,EAAE;GACrC,MAAM,QAAQ,gBAAgB;GAC9B,MAAM,SAAS,cAAc,EAAE,MAAM,EAAE,MAAM,MAAO;AACpD,OAAI,WAAW,EACb,QAAO;;AAGX,SAAO;GACP"}