# lace:sort

Drag-and-drop reordering with automatic server sync. For the full guide with live demos
(drag handles, cross-list dragging), see **[Sorting](/docs/sorting/)**.

## Syntax

```django
<ul lace:sort="reorder($item, $position)">
    {% for item in items %}
        <li lace:sort:item="{{ item.id }}" lace:key="item-{{ item.id }}">
            <span lace:sort:handle>☰</span>
            {{ item.title }}
        </li>
    {% endfor %}
</ul>
```

## Directives

| Directive | Purpose |
|---|---|
| `lace:sort="method($item, $position)"` | Container. Calls the method when items reorder. |
| `lace:sort:item="key"` | Marks a sortable child with a unique key. |
| `lace:sort:handle` | Optional drag handle (without it, the whole item drags). |
| `lace:sort:group="name"` | Enable dragging between containers sharing a group. |
| `lace:sort:ignore` | Prevent an element from being dragged. |

Sortable children need [`lace:key`](/docs/lace-key/) so their identity survives the morph.

## Example

```python
class TaskList(Component):
    tasks: list = []

    def reorder(self, task_id, new_position):
        task = next((t for t in self.tasks if t["id"] == int(task_id)), None)
        if task:
            self.tasks.remove(task)
            self.tasks.insert(int(new_position), task)
```
