skip to content

Sorting a column of size labels held as codes can give three different orders — what decides which one you get?

level: middleimportance: should knowfreq 49%

answer

  1. more than one candidate order
  2. values, declared set, or codes
  3. first-appearance order is data-dependent
  4. some designs refuse to order at all
  5. declare the order where the table is built

basics

~20 s

Three sources compete: the values' own comparison order, an order declared on the column's value set, and the stored codes as assigned. Which one the design consults decides whether small, medium, large sorts in that order.

solid answer

~50 s

A dictionary-coded column — each distinct value kept once, with a short whole number per row pointing at it — has more than one thing that could define an order, and that is the whole trap. The comparison can run on the **values themselves**, in which case size labels sort the way text sorts and small, medium, large comes back alphabetical. It can run on an **order declared on the value set**, which is the only source that gives the order you meant. Or it can run on the **stored codes**, which are usually assigned as values are first encountered, so the sort order becomes the order the data happened to arrive in. Some designs add a fourth behaviour: they treat a value set with no declared order as genuinely unordered and refuse a comparison such as *greater than medium* outright.

go deeper

for a junior

Recall that a coded column can sort in an order that is not the one you meant, and that the fix is to state the intended order rather than to rely on how the labels happen to compare.

for a middle

Name the three sources — the values, an order declared on the value set, the stored codes — and explain why a code-based order is data-dependent while the other two are not.

for a senior

Show the diagnosis: build the same values in two arrival orders and sort both. Differing results mean the comparison is on codes, which makes the output a function of upstream ordering.

for a principal

The angle is where the order lives. An order declared once on the value set is a property of the data; an order re-established at each use site is a convention, and conventions decay across teams.

## Three places an order can come from A plain text column has exactly one candidate order: the one the values themselves define. A **dictionary-coded column** — each distinct value held once, with a short whole number per row pointing at it — has three, because it now carries structure the plain column did not. | Source of the order | How the order is decided | small, medium, large comes back as | |---|---|---| | The values themselves | The values are compared as values | whatever their natural comparison gives — for text, alphabetical: large, medium, small | | An order declared on the value set | You state the order when the set is defined, and it overrides the values' own | small, medium, large | | The stored codes | Codes are compared as whole numbers, and are usually assigned as values are first encountered | whatever order the rows happened to arrive in | All three are defensible designs. None of them is *the* answer, which is why the question is worth asking: a candidate who says 'it sorts by the values' has only ever used one design and has not noticed. ## And a fourth behaviour: refusing Some designs treat a value set with no declared order as genuinely unordered, and refuse an ordering comparison such as *greater than medium* rather than answering it from the codes. This is the safest of the four behaviours and the one that surprises people most, because the same expression worked on the plain text column and stopped working the moment the column was coded. Refusal is not a bug to route around; it is the design telling you it has no order to give and wants you to state one. ## Why it bites in production The failure is quiet. An ordered report lists sizes in the wrong sequence, a minimum or a maximum returns the wrong member, or a condition such as *at least medium* selects a set nobody checked. Nothing raises, because every one of the three orders is a real order — just not the one the business meant. And the code-based order has a nastier property than the value-based one: it depends on the data, so the same program can produce two different orders on two different input files, and a change of upstream sort order silently reorders your output. ## How to tell which one you have 1. **Read the column's value set** and check whether it carries a declared order at all. If it does, that is almost certainly what the comparison is using. 2. **Ask the column for its minimum or maximum** and see whether it refuses. A refusal tells you the design will not order an unordered value set, which is useful information rather than an obstacle. 3. **Build the same values twice, in two different arrival orders, and sort both.** If the two results differ, the comparison is consulting the stored codes. If they agree but the order is alphabetical rather than the one you meant, it is consulting the values. That third test is the one worth remembering, because it distinguishes the two wrong answers from each other without reading any documentation. ## Making the order a property of the column The durable repair is to declare the order on the value set where the table is built, so it travels with the column instead of being re-established at each use site. Where a design does not offer a declared order, the alternatives are to carry a separate small column holding a rank per value and sort on that, or to decode and sort the values with an explicit comparison. Both work; both have to be repeated everywhere the column is used, and either one is forgotten eventually. A few related points worth having ready: - **Declaring the order is separate from declaring the set.** A design can let you state which values are permitted without letting you state how they compare, and then equality is safe while ordering is not. - **Adding a member later does not necessarily place it.** If a new size arrives, a declared order has to say where it goes; a code-based order will simply put it wherever it was first seen, which is usually last. - **Equality is unaffected by all of this.** Whichever source defines the order, testing a row against one particular value is the same question and gives the same answer. ## What the interviewer is listening for The sentence they want is *it depends where the order comes from*, followed by the three sources named. A candidate who then says which one they would rely on, and how they would check it on an unfamiliar column, has demonstrated the thing this question exists to find: that they know one column can sort three different ways, and that they have been caught by it.

  • Which of the three orders is the dangerous one, and why?
    The one taken from the stored codes. The values' own order is at least stable and predictable, while codes are usually assigned as values are first encountered, so the sort order depends on the input data. The same program on two files can then emit two different orders with nothing raised.
  • A new size label arrives after the value set was declared with an order. What happens?
    It depends on the design: the value may be refused, recorded as absent, or appended with no defined place in the order. None of those is automatically right, which is why an ordered value set is a small contract that needs revisiting when the business adds a value.

saying these in an interview costs you the question

  • Says a coded column always sorts by its values
  • Assumes the stored codes reflect the values' own order
  • Treats first-appearance code order as stable across input files
  • Thinks a refusal to order an unordered value set is a defect
  • Fixes it by renaming values so the text sorts correctly