logo

G2

  • Chart Gallery
  • Docs
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.3
  • Get Started
  • Introduction
    • What is G2
    • Using in Frontend Frameworks
    • Experimental Spec API
  • Chart API
  • Core Concepts
    • Chart
      • Components of G2 Charts
      • How to Use Charts
    • Mark
      • Overview
      • area
      • box
      • boxplot
      • cell
      • chord
      • density
      • gauge
      • heatmap
      • image
      • interval
      • line
      • lineX
      • lineY
      • link
      • liquid
      • sunburst
      • point
      • polygon
      • range
      • rangeX
      • rangeY
      • rect
      • shape
      • text
      • vector
      • connector
      • wordCloud
    • View
    • Data
      • Overview
      • custom
      • ema
      • fetch
      • filter
      • fold
      • inline
      • join
      • kde
      • log
      • map
      • pick
      • rename
      • slice
      • sort
      • sortBy
    • Encode
    • Scale
      • Overview
      • band
      • linear
      • log
      • ordinal
      • point
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
      • pow
    • Transform
      • Overview
      • bin
      • binX
      • diffY
      • dodgeX
      • flexX
      • group
      • groupColor
      • groupX
      • groupY
      • jitter
      • jitterX
      • jitterY
      • normalizeY
      • pack
      • sample
      • select
      • selectX
      • selectY
      • sortColor
      • sortX
      • sortY
      • stackEnter
      • stackY
      • symmetryY
    • Coordinate
      • Overview
      • fisheye
      • parallel
      • polar
      • theta
      • transpose
      • radial
      • cartesian3D
      • helix
    • Style
    • Animate
      • Overview
      • fadeIn
      • fadeOut
      • growInX
      • growInY
      • morphing
      • pathIn
      • scaleInX
      • scaleInY
      • scaleOutX
      • scaleOutY
      • waveIn
      • zoomIn
      • zoomOut
    • State
    • Interaction
      • Overview
      • brushAxisHighlight
      • brushHighlight
      • brushXHighlight
      • brushYHighlight
      • brushFilter
      • brushXFilter
      • brushYFilter
      • chartIndex
      • elementHighlight
      • elementHighlightByColor
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
      • fisheye
    • Composition
      • Overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • Overview
      • academy
      • classic
      • classicDark
    • Events
    • Color Mapping
  • Chart Component
    • Title
    • Axis
    • Legend
    • Scrollbar
    • Slider
    • Tooltip
    • Data Label
  • Extra Topics
    • Graph
      • forceGraph
      • pack
      • sankey
      • tree
      • treemap
    • Geo
      • geoPath
      • geoView
    • 3D
      • Draw 3D Chart
      • point3D
      • line3D
      • interval3D
      • surface3D
    • Plugin
      • renderer
      • rough
      • lottie
      • a11y
    • Package on demand
    • Set pattern
    • Server-Side Rendering (SSR)
    • Spec Function Expression Support (Available in 5.3.0)
  • Whats New
    • New Version Features
    • Migration from v4 to v5
  • Frequently Asked Questions (FAQ)

sortX

Previous
sortColor
Next
sortY

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...

Overview

sortX is a common data transform provided by G2, used for sorting the domain of discrete x-axis. By specifying sorting criteria, you can arrange the chart's x-axis in ascending or descending order according to certain metrics (such as y, color, size, etc.), making it more intuitive to display data size relationships or distribution trends.
sortX supports flexible sorting channels, aggregation methods, slicing configurations, and is commonly used for highlighting key points, optimizing readability, and comparative analysis. It is especially suitable for marks with discrete x channels (such as interval, rect, point, etc.).


Use Cases

  • Bar/Column Chart Sorting: Arrange bars from high to low (or low to high) by values for easier data comparison.
  • Group/Facet Sorting: In grouped or faceted scenarios, sort by certain metrics within groups to highlight differences between groups.
  • Top N/Bottom N Filtering: Combined with slice configuration, display only the top N or bottom N items to focus on key points.
  • Use with Other Transforms: Often used in combination with other transforms like dodgeX, diffY, etc., to achieve more complex data layouts and visual effects.

Configuration Options

PropertyDescriptionTypeDefault
bySpecify the sorting channel (e.g., 'y', 'color', 'size', etc.)string'y'
reverseWhether to reverse the orderbooleanfalse
sliceSelect a slice range (e.g., top N items, interval)number | [number, number]
reducerAggregation method for grouped comparisonsReducer'max'
ordinalWhether to handle as categorical channel (set to false for continuous data)booleantrue

by

Specifies the channel to sort by, commonly used values include 'y' (sort by y values), 'color' (sort by color groups), 'size' (sort by point size), etc.

reverse

Whether to reverse the sort order. true means reverse the sorting result, false means keep the default order. The actual sorting direction also relates to the reducer aggregation method (e.g., when reducer: 'max', reverse: true means descending; when reducer: 'min', reverse: true means ascending).

slice

Used to extract part of the sorted data. Can be a number (top N items) or an interval [start, end].

reducer

When the sorting criterion is an array or grouped, specifies the aggregation method. Supports 'max', 'min', 'sum', 'mean', 'median', 'first', 'last', or custom functions.

ordinal

Whether to handle as categorical channel. Can be set to false if the sorting criterion is continuous data.

type Reducer =
| 'max'
| 'min'
| 'sum'
| 'first'
| 'last'
| 'mean'
| 'median'
| ((I: number[], V: Primitive[]) => Primitive);

Examples

1. Bar Chart Sorted by y Values in Descending Order

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ Category: 'A', Value: 30 },
{ Category: 'B', Value: 50 },
{ Category: 'C', Value: 20 },
{ Category: 'D', Value: 40 },
],
encode: { x: 'Category', y: 'Value' },
transform: [{ type: 'sortX', by: 'y', reverse: true }],
});
chart.render();

2. Show Only Top 3 Items (slice)

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ Category: 'A', Value: 30 },
{ Category: 'B', Value: 50 },
{ Category: 'C', Value: 20 },
{ Category: 'D', Value: 40 },
],
encode: { x: 'Category', y: 'Value' },
transform: [{ type: 'sortX', by: 'y', reverse: true, slice: 3 }],
});
chart.render();

3. Used with dodgeX, Sorting After Grouping

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ State: 'A', Age: 'Youth', Population: 30 },
{ State: 'A', Age: 'Middle-aged', Population: 40 },
{ State: 'A', Age: 'Elderly', Population: 20 },
{ State: 'B', Age: 'Youth', Population: 50 },
{ State: 'B', Age: 'Middle-aged', Population: 60 },
{ State: 'B', Age: 'Elderly', Population: 30 },
],
encode: { x: 'State', y: 'Population', color: 'Age' },
transform: [{ type: 'sortX', by: 'y', reverse: true }, { type: 'dodgeX' }],
});
chart.render();

4. Complex Sorting with Reducer Configuration

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ Category: 'A', Value: 30 },
{ Category: 'B', Value: 50 },
{ Category: 'C', Value: 20 },
{ Category: 'D', Value: 40 },
],
encode: { x: 'Category', y: 'Value' },
transform: [
{ type: 'sortX', by: 'y', reducer: 'min' }, // Sort by minimum value
],
});
chart.render();