common-stuff
    Preparing search index...

    Function sortBy

    • Stable sort using provided callback without modifications. A custom key function can be supplied to customize the sort order. Key can return any nested data structure, supports number, booleans, arrays, objects.

      Type Parameters

      • T

      Parameters

      • arr: T[]

        any array

      • Optionalkey: (item: T) => unknown

        a function to execute to decide the order

      Returns T[]

      sorted array copy

      // Sort by object key
      sortBy(arr, v => v.myKey))
      // Sort by number in reversed order
      sortBy(arr, v => v.numberKey * -1))
      // Sort by object key, then by boolean value in revered order
      sortBy(arr, v => [v.myKey, !v.boolValue])
    • Stable sort using provided callback without modifications. A custom key function can be supplied to customize the sort order. Key can return any nested data structure, supports number, booleans, arrays, objects.

      Type Parameters

      • T

      Parameters

      • arr: readonly T[]

        any array

      • Optionalkey: (item: T) => unknown

        a function to execute to decide the order

      Returns readonly T[]

      sorted array copy

      // Sort by object key
      sortBy(arr, v => v.myKey))
      // Sort by number in reversed order
      sortBy(arr, v => v.numberKey * -1))
      // Sort by object key, then by boolean value in revered order
      sortBy(arr, v => [v.myKey, !v.boolValue])