logo

S2

  • Manual
  • API
  • Examples
  • Playground
  • ChangeLog
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quick Start
  • Basic Tutorial
    • Base Concept
    • Sheet Type
      • Pivot Mode
        Updated
      • Table Mode
    • Conditions
      Updated
    • Totals
    • Sort
      • Custom Sort
        Updated
      • Group Sort
        Updated
    • Theme
      Updated
    • Tooltip
      Updated
    • Internationalization
    • 数据格式化
      New
    • Multi Line
      New
    • Pagination
      New
  • Advanced Tutorial
    • Advanced Tutorial
      • Link
      • Render Chart In Cell
      • Draw Chart With @antv/g2
    • Custom
      • Customize Hook
        Updated
      • Customize Tree
        New
      • Customize Icon
      • Customize Cell Alignment
      • Customize Cell Size
        Updated
      • Customize Order
      • Custom Collapse/Expand Nodes
    • Interaction
      • Basic Interaction
      • Custom Interaction
      • Hide Columns
      • Cell Resize
      • Merge Cell
      • Scroll
      • Copy
        New
      • Highlight and select cell
        New
    • Analyze Component
      • Introduction
        New
      • Advanced Sort
        Updated
      • Switcher
      • Export
        Updated
      • Pagination
      • Drill Down
    • Sheet Component
      • Editable Mode
      • Strategy Sheet
        Updated
    • HD Adapter
    • Get Instance
    • Get Cell Data
      Updated
    • Table adaptive
    • AntV/G Plugins
      New
    • Pivot Chart
      Experimental
    • Vue 3.0
  • Extended Reading
    • Data Process
      • Pivot
      • Table
    • Layout
      • Pivot
      • Table
    • Performance
      Updated
  • Contribution
  • FAQ
  • S2 2.0 Migration Guide

Custom Sort

Previous
Totals
Next
Group Sort

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Introduction

Customize the sorting of table data rows, support row header/column header sorting, single row/column sorting, custom lists, functions and other functions. Check out the examples .

use

Driven by passing sortParams data in s2DataConfig

sortParam

parameterillustratetypeDefaultsrequired
sortFieldIdMeasure Id, the Id to be sortedstring-✓
sortMethodsort byASC | DESC | asc | desc-
sortBycustom sorted liststring[]-
sortByMeasureSort by metric value (numeric value) (for pivot tables)string-
queryFilter criteria, narrow the sorting range such as: { city: '成都' }object-
typeSorting within the group is used to display icons (applicable to pivot tables)string-
sortFuncFunction for custom sorting(v: SortFuncParam) => Array<string>-
import { EXTRA_FIELD } from "@antv/s2";
const s2DataConfig = {
sortParams: [
{
sortFieldId: 'type',
sortMethod: 'DESC',
// EXTRA_FIELD 是 dataCfg.fields.values 字段的虚拟 fieldId
query: { city: '成都', [EXTRA_FIELD]: 'price' },
},
],
...
};

Way

1. Ascending/descending method (sortMethod)

Row/column header dimensions are supported, and the ascending and descending order is based on the first letter . Examples are as follows:

sortParams: [
{ sortFieldId: 'province', sortMethod: 'DESC' },
{ sortFieldId: 'type', sortMethod: 'ASC' },
]
row

2. List of dimension values ​​(sortBy)

Supports setting the corresponding list in the table for the行/列头. If there is nesting, the sub-dimensions will be sorted within the group (as shown in city ), for example:

sortParams: [
{ sortFieldId: 'province', sortBy: [ '吉林', '浙江' ] },
{ sortFieldId: 'city', sortBy: [ '舟山', '杭州', '白山', '长春' ] },
{ sortFieldId: 'type', sortBy: [ '纸张', '笔' ] },
];
row

3. Measure field (sortByMeasure)

Support行头/列头sorting based on metric (numeric) data

row/column

Supports sorting by行或列, examples are as follows:

const s2DataConfig = {
sortParams: [
{
// type 依据 浙江-舟山-price 升序 排序
sortFieldId: 'type',
sortMethod: 'ASC',
sortByMeasure: 'price',
query: {
province: '浙江',
city: '舟山',
[EXTRA_FIELD]: 'price',
},
},
]
}
row

row+column

Supports sorting by行+列, examples are as follows:

const s2DataConfig = {
sortParams: [
{
// type 依据( 浙江 - 舟山 )&( price ) 升序 排序
sortFieldId: 'type',
sortMethod: 'ASC',
sortByMeasure: 'price',
query: {
province: '浙江',
city: '舟山',
[EXTRA_FIELD]: 'price',
},
},
]
}
row

4. Summary value

The non-leaf node of the行/列头. At this time, sortByMeasure is the summary virtual field TOTAL_VALUE, and the value is $$total$$ .

Configure Data Aggregation Mode

  1. Use aggregated data from data.
  2. Use aggregate calculations available in S2. ​📊 View document subtotal total configuration

Row Total/Row Subtotal

Sort column headers by行总计/行小计, examples are as follows:

Row subtotal :

import { TOTAL_VALUE, EXTRA_FIELD } from "@antv/s2";
const s2DataConfig = {
sortParams: [
{
// type 依据 ( 浙江 - 小计 )&( price )& 降序 排序
sortFieldId: 'type',
sortMethod: 'DESC',
sortByMeasure: TOTAL_VALUE,
query: {
province: '浙江',
[EXTRA_FIELD]: 'price',
},
},
];
}
// 使用前端总计的聚合方法进行排序。如果 data 数据中存在聚合数据则使用
const s2Options = {
totals: {
row: {
subTotalsDimensions: [ 'province' ],
calcSubTotals: {
aggregation: 'SUM'
}
}
}
}
row

row totals:

import { TOTAL_VALUE, EXTRA_FIELD } from "@antv/s2";
const s2DataConfig = {
sortParams: [
{
// 对 type 中笔和纸的总计进行 降序 排序
sortFieldId: 'type',
sortMethod: 'DESC',
sortByMeasure: TOTAL_VALUE,
query: {
[EXTRA_FIELD]: 'price',
},
},
],
// data 中带有排序使用的数据时,S2 会优先使用 data 返回的数据进行排序
data:[
{
type: "笔",
price: "38"
},
{
type: "纸张",
price: "36"
}
]
}
rowTotal

Column Total/Column Subtotal

Sort row headers by列总计/列小计, examples are as follows:

import { TOTAL_VALUE, EXTRA_FIELD } from "@antv/s2";
const s2DataConfig = {
sortParams: [
{
// province 依据( province - 小计 )&( 总计 - price )& 升序 排序
sortFieldId: 'province',
sortMethod: 'ASC',
sortByMeasure: TOTAL_VALUE,
query: {
[EXTRA_FIELD]: 'price',
},
}
]
}
const s2Options = {
totals: {
row: {
subTotalsDimensions: ['province'],
calcSubTotals: {
aggregation: 'SUM',
},
}
}
}
row

When there is a subtotal in the列小计, add the corresponding parameter in the query to get the corresponding cell, same行小计above

5. Custom method (sortFunc)

sortFunc will return SortFuncParam parameter according to the current conditions, and supports two ways of维度值and度量值

parameterillustratetypeDefaultsrequired
sortFieldIdMeasure Id, the Id to be sortedstring-✓
sortMethodsort byASC | DESC | asc | desc-
sortBycustom sorted liststring[]-
sortByMeasureSort by metric value (numeric value)string-
queryFilter criteria, narrow the sort range such as: {city:'白山'}object-
typeSorting within the group is used to display the iconstring-
dataList of currently sorted data`Array<stringRecord<string, any>>`-

Dimension value (row/column header)

Support dimension value customization, that is, row header or column header, examples are as follows:

const s2DataConfig = {
sortParams: [
{
// sortFieldId 为维度值时,params.data 为维度值列表
sortFieldId: 'province',
sortFunc: (params) => {
const { data } = params;
return data.sort((a, b) => a.localeCompare(b));
},
},
]
}
row

measure (numeric)

Supports custom calculations using metrics, for example:

const s2DataConfig = {
sortParams: [
{
sortFieldId: 'city',
sortByMeasure: 'price',
// 当使用 sortByMeasure 时,可以传入 query 定位数值列表
// 如下方限定 params.data 为 type=纸张,数值=price 的数据
query: { type: '纸张', [EXTRA_FIELD]: 'price' },
sortFunc: (params) => {
const { data, sortByMeasure, sortFieldId } = params || {};
return data
// 使用 price 做比较
?.sort((a, b) => b[sortByMeasure] - a[sortByMeasure])
// map 出 city 维度的数组
?.map((item) => item[sortFieldId]);
},
},
]
}
row

📊 View demo custom sorting .

priority

  1. The condition priority in sortParams is higher than the original data
  2. sortParams multiple item : according to the order priority, the priority of the latter is higher
  3. Multiple conditions in item : sortByMeasure > sortFunc > sortBy > sortMethod