MCPcopy Create free account
hub / github.com/TanStack/db / sortBy

Function sortBy

packages/db-collection-e2e/src/utils/helpers.ts:165–178  ·  view source on GitHub ↗
(
  array: Array<T>,
  field: K,
  direction: `asc` | `desc` = `asc`,
)

Source from the content-addressed store, hash-verified

163 * Sort array of objects by a field
164 */
165export function sortBy<T, K extends keyof T>(
166 array: Array<T>,
167 field: K,
168 direction: `asc` | `desc` = `asc`,
169): Array<T> {
170 return [...array].sort((a, b) => {
171 const aVal = a[field]
172 const bVal = b[field]
173
174 if (aVal < bVal) return direction === `asc` ? -1 : 1
175 if (aVal > bVal) return direction === `asc` ? 1 : -1
176 return 0
177 })
178}
179
180/**
181 * Filter array by predicate function

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected