( array: Array<T>, field: K, direction: `asc` | `desc` = `asc`, )
| 163 | * Sort array of objects by a field |
| 164 | */ |
| 165 | export 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 |
nothing calls this directly
no outgoing calls
no test coverage detected