📄 tanstack/table/v8/docs/framework/qwik/examples/sorting

File: sorting.md | Updated: 11/15/2025

Source: https://tanstack.com/table/v8/docs/framework/qwik/examples/sorting



TanStack

Table v8v8

Search...

+ K

Auto

Log In

TanStack StartRC

Docs Examples GitHub Contributors

TanStack Router

Docs Examples GitHub Contributors

TanStack Query

Docs Examples GitHub Contributors

TanStack Table

Docs Examples Github Contributors

TanStack Formnew

Docs Examples Github Contributors

TanStack DBbeta

Docs Github Contributors

TanStack Virtual

Docs Examples Github Contributors

TanStack Paceralpha

Docs Examples Github Contributors

TanStack Storealpha

Docs Examples Github Contributors

TanStack Devtoolsalpha

Docs Github Contributors

More Libraries

Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide

Documentation

Framework

Qwik logo

Qwik

Version

v8

Search...

+ K

Menu

Getting Started

Core Guides

Feature Guides

Core APIs

Feature APIs

Enterprise

Examples

Framework

Qwik logo

Qwik

Version

v8

Menu

Getting Started

Core Guides

Feature Guides

Core APIs

Feature APIs

Enterprise

Examples

Qwik Example: Sorting

Github StackBlitz CodeSandbox

===============================================================================================================================================================================================================================================================================================================================================================================================

Code ExplorerCode

Interactive SandboxSandbox

  • src

    • index.css file iconindex.css

    • main.tsx file iconmain.tsx

    • makeData.ts file iconmakeData.ts

    • vite-env.d.ts file iconvite-env.d.ts

  • .gitignore file icon.gitignore

  • README.md file iconREADME.md

  • index.html file iconindex.html

  • package.json file iconpackage.json

  • tsconfig.json file icontsconfig.json

  • tsconfig.node.json file icontsconfig.node.json

  • vite.config.ts file iconvite.config.ts

tsx

import '@builder.io/qwik/qwikloader.js'
import { render, component$, useSignal, $ } from '@builder.io/qwik'

import './index.css'
import { makeData } from './makeData'

import {
  getCoreRowModel,
  flexRender,
  useQwikTable,
  SortingState,
  ColumnDef,
  getSortedRowModel,
} from '@tanstack/qwik-table'

type Person = {
  firstName: string
  lastName: string
  age: number
  visits: number
  status: string
  progress: number
}

const columns: ColumnDef<Person>[] = [\
  {\
    accessorKey: 'firstName',\
    cell: info => info.getValue(),\
    footer: props => props.column.id,\
  },\
  {\
    accessorFn: row => row.lastName,\
    id: 'lastName',\
    cell: info => info.getValue(),\
    header: () => <span>Last Name</span>,\
    footer: props => props.column.id,\
  },\
  {\
    accessorKey: 'age',\
    header: () => 'Age',\
    footer: props => props.column.id,\
  },\
  {\
    accessorKey: 'visits',\
    header: () => <span>Visits</span>,\
    footer: props => props.column.id,\
  },\
  {\
    accessorKey: 'status',\
    header: 'Status',\
    footer: props => props.column.id,\
  },\
  {\
    accessorKey: 'progress',\
    header: 'Profile Progress',\
    footer: props => props.column.id,\
    sortDescFirst: true, // This column will sort in descending order first (default for number columns anyway)\
  },\
  {\
    accessorKey: 'createdAt',\
    header: 'Created At',\
    cell: info => info.getValue<Date>().toLocaleDateString(),\
    // sortingFn: 'datetime' (inferred from the data)\
  },\
]

const App = component$(() => {
  const data = useSignal(makeData(10_000))

  const sorting = useSignal<SortingState>([])

  const table = useQwikTable({
    columns,
    data: data.value,
    getCoreRowModel: getCoreRowModel(),
    getSortedRowModel: getSortedRowModel(),
    enableSorting: true,
    state: {
      sorting: sorting.value,
    },
    onSortingChange: updater => {
      sorting.value =
        updater instanceof Function ? updater(sorting.value) : updater
    },
  })

  return (
    <div class="p-2">
      <table>
        <thead>
          {table.getHeaderGroups().map(headerGroup => (
            <tr key={headerGroup.id}>
              {headerGroup.headers.map(header => {
                const { column } = header
                const id = column.id
                return (
                  <th key={header.id} colSpan={header.colSpan}>
                    {header.isPlaceholder ? null : (
                      <div
                        class={
                          column.getCanSort()
                            ? 'cursor-pointer select-none'
                            : ''
                        }
                        onClick$={$(event => {
                          const col = table.getColumn(id)! //avoid serializing errors
                          col.getToggleSortingHandler()!(event)
                        })}
                        title={
                          column.getCanSort()
                            ? column.getNextSortingOrder() === 'asc'
                              ? 'Sort ascending'
                              : column.getNextSortingOrder() === 'desc'
                                ? 'Sort descending'
                                : 'Clear sort'
                            : undefined
                        }
                      >
                        {flexRender(
                          header.column.columnDef.header,
                          header.getContext()
                        )}
                        {{
                          asc: ' 🔼',
                          desc: ' 🔽',
                        }[header.column.getIsSorted() as string] ?? null}
                      </div>
                    )}
                  </th>
                )
              })}
            </tr>
          ))}
        </thead>
        <tbody>
          {table
            .getRowModel()
            .rows.slice(0, 10)
            .map(row => {
              return (
                <tr key={row.id}>
                  {row.getVisibleCells().map(cell => {
                    return (
                      <td key={cell.id}>
                        {flexRender(
                          cell.column.columnDef.cell,
                          cell.getContext()
                        )}
                      </td>
                    )
                  })}
                </tr>
              )
            })}
        </tbody>
      </table>
      <div>{table.getRowModel().rows.length.toLocaleString()} Rows</div>
      <pre>{JSON.stringify(sorting, null, 2)}</pre>
    </div>
  )
})

render(document.getElementById('app') as HTMLElement, <App />)


import '@builder.io/qwik/qwikloader.js'
import { render, component$, useSignal, $ } from '@builder.io/qwik'

import './index.css'
import { makeData } from './makeData'

import {
  getCoreRowModel,
  flexRender,
  useQwikTable,
  SortingState,
  ColumnDef,
  getSortedRowModel,
} from '@tanstack/qwik-table'

type Person = {
  firstName: string
  lastName: string
  age: number
  visits: number
  status: string
  progress: number
}

const columns: ColumnDef<Person>[] = [\
  {\
    accessorKey: 'firstName',\
    cell: info => info.getValue(),\
    footer: props => props.column.id,\
  },\
  {\
    accessorFn: row => row.lastName,\
    id: 'lastName',\
    cell: info => info.getValue(),\
    header: () => <span>Last Name</span>,\
    footer: props => props.column.id,\
  },\
  {\
    accessorKey: 'age',\
    header: () => 'Age',\
    footer: props => props.column.id,\
  },\
  {\
    accessorKey: 'visits',\
    header: () => <span>Visits</span>,\
    footer: props => props.column.id,\
  },\
  {\
    accessorKey: 'status',\
    header: 'Status',\
    footer: props => props.column.id,\
  },\
  {\
    accessorKey: 'progress',\
    header: 'Profile Progress',\
    footer: props => props.column.id,\
    sortDescFirst: true, // This column will sort in descending order first (default for number columns anyway)\
  },\
  {\
    accessorKey: 'createdAt',\
    header: 'Created At',\
    cell: info => info.getValue<Date>().toLocaleDateString(),\
    // sortingFn: 'datetime' (inferred from the data)\
  },\
]

const App = component$(() => {
  const data = useSignal(makeData(10_000))

  const sorting = useSignal<SortingState>([])

  const table = useQwikTable({
    columns,
    data: data.value,
    getCoreRowModel: getCoreRowModel(),
    getSortedRowModel: getSortedRowModel(),
    enableSorting: true,
    state: {
      sorting: sorting.value,
    },
    onSortingChange: updater => {
      sorting.value =
        updater instanceof Function ? updater(sorting.value) : updater
    },
  })

  return (
    <div class="p-2">
      <table>
        <thead>
          {table.getHeaderGroups().map(headerGroup => (
            <tr key={headerGroup.id}>
              {headerGroup.headers.map(header => {
                const { column } = header
                const id = column.id
                return (
                  <th key={header.id} colSpan={header.colSpan}>
                    {header.isPlaceholder ? null : (
                      <div
                        class={
                          column.getCanSort()
                            ? 'cursor-pointer select-none'
                            : ''
                        }
                        onClick$={$(event => {
                          const col = table.getColumn(id)! //avoid serializing errors
                          col.getToggleSortingHandler()!(event)
                        })}
                        title={
                          column.getCanSort()
                            ? column.getNextSortingOrder() === 'asc'
                              ? 'Sort ascending'
                              : column.getNextSortingOrder() === 'desc'
                                ? 'Sort descending'
                                : 'Clear sort'
                            : undefined
                        }
                      >
                        {flexRender(
                          header.column.columnDef.header,
                          header.getContext()
                        )}
                        {{
                          asc: ' 🔼',
                          desc: ' 🔽',
                        }[header.column.getIsSorted() as string] ?? null}
                      </div>
                    )}
                  </th>
                )
              })}
            </tr>
          ))}
        </thead>
        <tbody>
          {table
            .getRowModel()
            .rows.slice(0, 10)
            .map(row => {
              return (
                <tr key={row.id}>
                  {row.getVisibleCells().map(cell => {
                    return (
                      <td key={cell.id}>
                        {flexRender(
                          cell.column.columnDef.cell,
                          cell.getContext()
                        )}
                      </td>
                    )
                  })}
                </tr>
              )
            })}
        </tbody>
      </table>
      <div>{table.getRowModel().rows.length.toLocaleString()} Rows</div>
      <pre>{JSON.stringify(sorting, null, 2)}</pre>
    </div>
  )
})

render(document.getElementById('app') as HTMLElement, <App />)

Row Selection

Partners Become a Partner

Code RabbitCode Rabbit CloudflareCloudflare AG GridAG Grid NetlifyNetlify NeonNeon WorkOSWorkOS ClerkClerk ConvexConvex ElectricElectric SentrySentry PrismaPrisma StrapiStrapi UnkeyUnkey

scarf analytics