Home / Blog / Sorting past Château

July 28, 2026· Wine World Map

Sorting past Château

Load the Médoc district page. You get 180-odd wineries, sorted ORDER BY name ASC. The first thirty entries all start with the word Château. Your eye slides down the C column looking for the meaningful part of each name, which is the second word. The sort works, in a technical sense. It's also useless.

Then it gets worse. About a fifth of the Bordeaux wineries in OSM are tagged just Beychevelle or Lafite Rothschild, with no Château prefix — because the mapper thought the name was obvious from context. Now your alphabetical list has Ausone, Beychevelle, Cos d'Estournel, and Ducru-Beaucaillou all appearing before the big block of Château-anything. The bare-named ones and the prefixed ones interleave incorrectly, and the human reading the page has no way to scan the list.

The prefixes are honorifics, not names

Every wine-producing culture has a word that goes in front of the producer's name and carries roughly zero identifying information. In practice we treat it as a stopword.

// scripts/lib/winery-sort.ts
export const WINERY_PREFIXES = [
  'Château', 'Chateau',
  'Domaine', 'Domaines', 'Dom.',
  'Weingut', 'Weinhaus', 'Weinbau',
  'Bodega', 'Bodegas', 'Bodegas y Viñedos',
  'Cantina', 'Cantine', 'Cantina Sociale',
  'Azienda Agricola', 'Az. Agr.', 'Az. Vitivinicola',
  'Tenuta', 'Tenute', 'Fattoria', 'Podere',
  'Castello', 'Villa', 'Casa',
  'Quinta', 'Herdade', 'Casal',
  'Clos', 'Mas',
  'Winery', 'Vineyards', 'Cellars', 'Estate',
]

const PATTERN = new RegExp(
  `^(?:${WINERY_PREFIXES.join('|')})\\s+`, 'i'
)

export function sortKey(name: string): string {
  return name
    .replace(PATTERN, '')
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '')
    .toLowerCase()
    .trim()
}

The trailing Winery, Cellars, Estate block is for the New World, where the honorific is a suffix instead of a prefix. That's a separate regex (\s+(Winery|Cellars|Estate|Vineyards)$) that runs on the same key. Between the two, you can flatten most of the world into comparable strings.

Where the key lives

Storing it computed on every query would work but stinks. Instead there's a sort_key column populated at import time, indexed for ORDER BY and for prefix search:

ALTER TABLE wineries ADD COLUMN sort_key TEXT;

UPDATE wineries SET sort_key = winery_sort_key(name);

CREATE INDEX wineries_sort_key_idx ON wineries (sort_key);
CREATE INDEX wineries_sort_key_trgm_idx
  ON wineries USING gin (sort_key gin_trgm_ops);

gin_trgm_ops is what makes fuzzy search behave when someone types "beychvelle" and expects to find Château Beychevelle. Search runs against sort_key first, name second, and merges the results. The honorifics don't pollute the trigram index.

What the list actually looks like

A slice of Médoc, before and after:

| Naive ORDER BY name | ORDER BY sort_key | |---|---| | Beychevelle | Angélus | | Château Ausone | Ausone | | Château Cheval Blanc | Beychevelle | | Château Lafite Rothschild | Cheval Blanc | | Château Latour | Cos d'Estournel | | Château Margaux | Ducru-Beaucaillou | | Cos d'Estournel | Lafite Rothschild | | Ducru-Beaucaillou | Latour | | Le Petit Cheval | Margaux | | Petrus | Petit Cheval, Le | | ... | Petrus |

The right column reads like a wine list. The left column reads like a database dump. That's the whole difference.

The Burgundy tax

Burgundy is worse than Bordeaux, because Domaine accounts for roughly 70 % of the producers and the surnames after it are often long and multi-word: Domaine de la Romanée-Conti, Domaine Leflaive, Domaine Armand Rousseau Père et Fils. The prefix regex has to be non-greedy about compound prefixes — Domaine de la shouldn't be stripped in one shot, because then Romanée-Conti sorts under R and you've lost the de la which is part of the identifier. We strip only Domaine\s+; the de la stays and sorts under D anyway, which is fine because it's still the meaningful part of the name.

Italian is the mirror problem. Azienda Agricola is two words and you need to strip both. Az. Agr. is the abbreviated form and shows up in about a third of OSM entries. Both go in the prefix list. Piemontese producers who call themselves Cantina on the label and Azienda Agricola on the invoice get inconsistent OSM tagging, and that's the dedup problem more than the sort problem.

Lesson

The prefix is heraldry, not identity. Sort by identity. Every domain has words that sound like they belong to the entity but are actually shared by an entire category — Château in Bordeaux, Domaine in Burgundy, Bodega in Rioja, Doctor in medical directories, Inc. in corporate filings. If you sort your list by the raw string, you're sorting by category first and identity second, and the human reading it will notice before the tests do.

#search#naming#postgres