Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

Orders as Categories — Why Comparison Matters More Than Sorting

When we compare things in programming, we instinctively think about sorting:

numbers, timestamps, scores…

But comparison is a much broader idea than sorting.

Sometimes we don’t need to arrange values from smallest to largest. Sometimes we only need to know whether one thing can stand in place of another.

Category theory captures this using the idea of order — and surprisingly, once you look closely, orders already behave like categories.

Starting With a Simple Relation

https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/orders_fp.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260225%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260225T014221Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=3f05787ffd420160bcba1f03e3c8514c27d1d0a7d989b3ad7d31033ed6ea257f694ac3708f3076a1575171aeb96596fe0051c8f0cae017556af8e8942f852127008ef4b00c4c1eb52a001c50f56d6da59f998d1170708f530aeefb5ee045cf9c3542d48f971d7c9560770153a1953b1b4c68295be97c549638d0502ec10cd02f19b5703828d19f8fdcb8661a19fc7e8561de3563e7d208eb8c83251e78305294552c18bd522b8f05463d6b1645ad45350e455410a750d19fa70bd092fdea6131ed346abdccb3aad15d89ebb808e3026eea32f855f50662cab340836c7dfbcf3560df99ac9668782211008cbd7368b08e53b021eff90ec87423ede3c067b66dbd

Take a set of elements and a relation ≤ between them.

We don’t assume numbers. We don’t assume sorting. We only assume the relation behaves consistently.

To be consistent, the relation must obey a few rules.

Rule 1 — Reflexive

Every element relates to itself.

a ≤ a

This just means comparison must never fail on identical values.

If a type cannot be used where the same type is expected, the system is already broken.

Rule 2 — Transitive

If a relates to b, and b relates to c, then a relates to c.

a ≤ b and b ≤ c ⇒ a ≤ c

This is what makes reasoning scalable.

Without it, every check would have to be repeated manually — nothing could safely compose.

When a relation satisfies just these two rules, we call it a preorder.

From Preorder to Partial Order

a ≤ b
b ≤ a

even when a and b are different.

To remove this ambiguity, we add one more rule:

If two elements relate both ways, they must actually be the same element.

This is called antisymmetry.

Now the relation becomes a partial order.

At this point the structure becomes stable enough to represent dependencies — no circular chains remain.

From Partial Order to Total Order

We strengthen the relation one more time.

For every pair of elements, one must relate to the other:

a ≤ b or b ≤ a

Now every pair can be compared.

This gives a total order — the kind required for sorting.

Sorting algorithms rely entirely on this property. Without it, they cannot consistently place elements.

Why This Forms a Category

Think of a ≤ b as meaning you can go from a to b.

Now three natural things happen:

  • You can always stay where you are → a ≤ a (that’s the identity)
  • If you can go from a to b and from b to c, you can go from a to c (that’s composition)
  • And it doesn’t matter how you group the steps — the result is the same (associativity)
  • Those are exactly the rules a category needs. So we didn’t invent a category here — we simply noticed that a normal ordering relation already behaves like one.

    What Is a Hom-Set (Without the Scary Name)

    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/homset.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260225%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260225T014221Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=2862c9b27e6ce6e985d93ef69a1f9ba0eb11963b0540306aeb11289d47eb290dfc67dc260031c78618052d365f1677e235d8d8eb8201fc4deafe216698254b5973786080e0dc0541849817edd779c3fde57c72314272831e4fc7d59c42d22779e3f1edc1c8c919ab185103314ce92a825884f71e8164a5938112992f59ff7ae986958c20e56e34ffaefafc5937e6748d89e4bea3d871f093c470c52060ec773117c86f04db025b897bd4584344de103d8a6e96fa06ece4a58faf225396bcf537d3382346f88bf9acf660298d595e21d386cadfb9dff9f48e1c798fb98879f36fc1216f7abddbaa4540dd03460fdf60ea654e77cb887c5a653f61788c267f9f58

    Forget the word hom-set for a moment. Imagine you ask a very practical question:

    “In how many ways can I go from A to B?”

    That’s it. All the possible paths from A to B — we collect them in one place. Category theory just gives that collection a name:

    Hom(A, B)

    You can read it as: “all valid arrows from A to B”

    In a Normal Programming Situation

    Between two types there can be many functions:

    String → Int

    You might have:

  • parseNumber
  • length
  • hashCode
  • digitCount
  • So:

    Hom(String, Int) = { parseNumber, length, hashCode, digitCount, ... }

    Many arrows live inside the hom-set.

    But In an Order Category

    Now remember — our arrows don’t represent functions anymore.

    They represent a statement:

    a ≤ b

    So we don’t ask:

    “Which function converts a to b?”

    We ask:

    “Is a allowed where b is expected?”

    Only two outcomes exist:

  • yes → arrow exists
  • no → arrow does not exist
  • So the hom-set shrinks dramatically:

    Hom(a, b) = { one arrow }   if a ≤ b
    Hom(a, b) = empty           if not

    The set no longer stores implementations. It stores permission.

    Why This Is Called a Thin Category

    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/thincat.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260225%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260225T014221Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=2d0f40ba662f12e274f4aba7e1032b93064f5591f68008bb4e3f2c72519a8782c3dc8c6c7504907d73abeeb6258ab7b87f0338dee910c1bc14287aa4c3373a08c920953255590475ca5458b925a6ce2c4991421b81b227bea4457eadba09e14ea5cc1a704e2b5c7809a367c79438f3fdfbcf73243677ff56a7653fd0a6368fe4a250cd2610d265ff6389f39a1f38fc65b1a0759479a350e78f65c3aaf9fe1d72e09e648fd1e7834a39c76013b508d7321161e819d1f280ac05793df6e026068e6c6f3917b56d6394884cedd3e12960dc6a7cd18edb577886bdca34647b5a62527ca70d04005a096cd176e05c3922dffd38feab5d5e705bb2f64e9ac8a8fa1d81

    Because every hom-set has at most one element.

    No alternatives. No competing arrows. No different implementations.

    Just true or false.

    That’s what “thin” means:

    there isn’t room for multiple arrows anymore

    Conclusion — More Than Sorting

    We began with a simple comparison: can this replace that?

    From that, we discovered structure:

  • consistent comparison → preorder
  • removing ambiguity → partial order
  • full comparability → total order
  • And surprisingly, this already satisfies the laws of a category.

    But this category is different.

    Its arrows don’t describe computation — they describe permission. Its hom-sets don’t hold functions — they hold truth.

    So ordering isn’t just about arranging values. It’s a way to reason about what programs are allowed to exist.

    #MathForDevelopers#FunctionalProgramming#CategoryTheory#TypeSystems#ProgrammingConcepts#Preorder#PartialOrder#TotalOrder#HomSet#ThinCategory#KotlinFP#SoftwareFoundations