Sorting

Sorting functionality allows users to sort data in spreadsheets for better organization and analysis. It supports various sorting methods, including ascending, descending, and custom sorting, helping users quickly find the data they need.

Preset Mode

Installation

Shell
pnpm add @univerjs/preset-sheets-sort

Usage

TypeScript
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsSortPreset } from '@univerjs/preset-sheets-sort'import UniverPresetSheetsSortEnUS from '@univerjs/preset-sheets-sort/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-sort/lib/index.css'const { univerAPI } = createUniver({  locale: LocaleType.EN_US,  locales: {    [LocaleType.EN_US]: mergeLocales(      UniverPresetSheetsCoreEnUS,      UniverPresetSheetsSortEnUS,     ),  },  presets: [    UniverSheetsCorePreset(),    UniverSheetsSortPreset(),   ],})

Plugin Mode

Installation

Shell
pnpm add @univerjs/sheets-sort @univerjs/sheets-sort-ui

Usage

TypeScript
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverSheetsSortPlugin } from '@univerjs/sheets-sort'import { UniverSheetsSortUIPlugin } from '@univerjs/sheets-sort-ui'import SheetsSortUIEnUS from '@univerjs/sheets-sort-ui/locale/en-US'import '@univerjs/sheets-sort-ui/lib/index.css'import '@univerjs/sheets-sort/facade'const univer = new Univer({  locale: LocaleType.EN_US,  locales: {    [LocaleType.EN_US]: mergeLocales(      SheetsSortUIEnUS,     ),  },})univer.registerPlugin(UniverSheetsSortPlugin)univer.registerPlugin(UniverSheetsSortUIPlugin)

Facade API

Complete Facade API type definitions can be found in the FacadeAPI.

Importing

Plugin mode note

Only plugin mode requires manually importing the Facade package. Preset mode already includes the corresponding Facade package, so no extra import is needed.

TypeScript
import '@univerjs/sheets-sort/facade'

Worksheet Sorting

FWorksheet.sort(colIndex, asc) method can sort the worksheet by the specified column.

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Sorts the worksheet in ascending order based on the first columnfWorksheet.sort(0)// Sorts the worksheet in descending order based on the first columnfWorksheet.sort(0, false)

Range Sorting

FRange.sort(column) method can sort the cells in a specified range by the given column and order.

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('D1:G10')// Sorts the range in ascending order based on the first columnfRange.sort(0)// Sorts the range in descending order based on the first columnfRange.sort({ column: 0, ascending: false })// Sorts the range in descending order based on the first column and ascending order based on the second columnfRange.sort([{ column: 0, ascending: false }, 1])

Event Listeners

Complete event type definitions can be found in the Events.

SheetRangeSorted event is triggered after the cell range sorting is completed.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.SheetRangeSorted, (params) => {  const { workbook, worksheet, range, sortColumn } = params})// Remove event listener using `disposable.dispose()`

SheetBeforeRangeSort event is triggered before the cell range sorting.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.SheetBeforeRangeSort, (params) => {  const { workbook, worksheet, range, sortColumn } = params  // Cancel the sorting operation  params.cancel = true})// Remove event listener using `disposable.dispose()`

How is this guide?

© 2026 DreamNum Co., Ltd.