# Numbers sort wrongly (1, 10, 2): VARCHAR column sorted as text

**Code:** `sorting` · **Category:** SQL · **Seen on:** SQL, MySQL, PostgreSQL

A numeric-looking column stored as text sorts alphabetically, so "10" comes before "2".

## Meaning

Text comparison is character by character. Store numbers in numeric columns, or cast in the query (`ORDER BY CAST(col AS INTEGER)`), and prefer natural-sort expressions/zero padding only as a stopgap. Same issue affects comparisons: `'10' < '9'`.

## Common causes

- Numeric data in VARCHAR/TEXT column
- CSV import without types
- Version strings sorted as text
- Mixed numeric and non-numeric values
- Collation affecting order

## Quick fix

1. Change the column type to INT/DECIMAL
2. `ORDER BY CAST(col AS INT)` as a workaround
3. Clean non-numeric values before converting

## Detailed fix by platform

### SQL

1. `SELECT * FROM items ORDER BY CAST(sku_number AS INTEGER);`

## How to diagnose

1. **Column type** — Text?
2. **Values** — Numeric only?
3. **Cast** — Safe?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/sql/varchar-numbers-sort-as-text)
