TanStackT
TanStack2mo ago
7 replies
slow-yellow

Possibility to sort array items

Is it possible to sort items in a form array?
We have this list of items inside a form which is displayed as a table.
if i sort the array like this
<form.Field name="transactions" mode="array">
                      {(field) => (
                        <TableBody>
                          {field.state.value
                            .toSorted((a, b) => {
                              if (sort === 'date')
                                return dir === 'asc'
                                  ? a.date.getTime() - b.date.getTime()
                                  : b.date.getTime() - a.date.getTime();
                              return 0;
                            })
                            .map((subField, i) => (
                              <TableRow
                                key={`transactions-${i}`}

whenever a user changes sorting the value changes he made are still set on the index he made them on.
For example a user updates transaction 2 in the table. If the user sorts the changes are still reflected on the 2nd transaction in the table altough this is a different transaction now.
Was this page helpful?