GitHub user FrancescoCastaldi added a comment to the discussion: How can i sort y axis with category name
When working with categorical bands (such as `'0-5 yrs'`, `'5-10 yrs'`, `'10-15 yrs'`, `'20+ yrs'`), standard alphabetical sorting fails because `'10-15 yrs'` precedes `'5-10 yrs'`. You can sort your categories in the correct logical sequence without displaying numeric prefixes (`1, 2, 3...`) in the chart using either of the following approaches: ### Method 1: Use a Custom SQL Metric in the "Sort By" Control (Recommended) In the chart configuration panel in Explore: 1. Keep your categorical column (`experience_band`) on the **X-axis** (or **Y-axis** in a horizontal bar chart). 2. Look at the **Sort By** / **Order by** control section. 3. Switch from selecting a pre-existing column to **Custom SQL**. 4. Enter the ordering logic using a `CASE` expression wrapped in an aggregate (such as `MIN` or `MAX`): ```sql MIN(CASE experience_band WHEN '0-5 yrs' THEN 1 WHEN '5-10 yrs' THEN 2 WHEN '10-15 yrs' THEN 3 WHEN '15-20 yrs' THEN 4 WHEN '20+ yrs' THEN 5 ELSE 99 END) ``` 5. Set the sort direction to **Ascending** (uncheck "Sort descending"). Because this SQL expression is added to the generated `ORDER BY` clause, the database orders the category buckets by their numeric rank (1, 2, 3...), while the axis labels display the clean string values (`0-5 yrs`, `5-10 yrs`...). ### Method 2: Dataset Calculated Column with Zero-Width Unicode Spaces If your chart plugin does not expose a separate Sort By metric when grouping categories: 1. In **Data -> Datasets -> Edit Dataset -> Calculated Columns**, create a new column named `experience_band_display`. 2. Prefix each string with varying numbers of invisible zero-width spaces (`U+200B`): ```sql CASE WHEN experience_band = '0-5 yrs' THEN CONCAT(CHAR(8203), '0-5 yrs') WHEN experience_band = '5-10 yrs' THEN CONCAT(CHAR(8203), CHAR(8203), '5-10 yrs') WHEN experience_band = '10-15 yrs' THEN CONCAT(CHAR(8203), CHAR(8203), CHAR(8203), '10-15 yrs') WHEN experience_band = '20+ yrs' THEN CONCAT(CHAR(8203), CHAR(8203), CHAR(8203), CHAR(8203), '20+ yrs') ELSE experience_band END ``` Alphabetical sorting will naturally sort by the invisible character count while human eyes in the browser see clean text labels. GitHub link: https://github.com/apache/superset/discussions/42790#discussioncomment-19164779 ---- This is an automatically sent email for [email protected]. To unsubscribe, please send an email to: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
