Menu
Coddy logo textTech

Comparing Objects (<, >)

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 29 of 70.

Beyond arithmetic and concatenation, you can also define how objects compare to each other using comparison metamethods. The __lt metamethod handles the "less than" (<code><) operator.

When Lua evaluates a < b, it looks for __lt in the metatable. This metamethod should return true or false based on your comparison logic.

local Item = {}
Item.__index = Item

function Item:new(name, price)
    local obj = {name = name, price = price}
    setmetatable(obj, Item)
    return obj
end

function Item.__lt(a, b)
    return a.price < b.price
end

With __lt defined, you can now compare items directly:

local apple = Item:new("Apple", 2)
local laptop = Item:new("Laptop", 999)

print(apple < laptop)   -- Output: true
print(laptop < apple)   -- Output: false

Here's the clever part: Lua automatically derives the operator from __lt. When you write a > b, Lua simply evaluates b < a instead. So defining just __lt gives you both comparison directions for free.

challenge icon

Challenge

Easy

Let's build a Product class that can be compared using the <code>< and operators based on price. This is perfect for scenarios like sorting items in a shopping cart or finding the cheapest option in a list.

You'll organize your code across two files:

  • Product.lua: Define your Product class with name and price fields. Include a :new(name, price) constructor and implement the __lt metamethod to compare products by their price. Remember that defining __lt automatically gives you the operator as well—Lua handles that for you!
  • main.lua: Require your Product module, create product instances from the inputs, and compare them to determine which costs more or less.

You will receive four inputs:

  1. First product's name
  2. First product's price
  3. Second product's name
  4. Second product's price

In your main file, create two products with the given names and prices. Then perform three comparisons and print the result of each on a separate line:

  1. Is the first product less than the second? (print true or false)
  2. Is the first product greater than the second? (print true or false)
  3. Print the name of the cheaper product

For example, if the inputs are Apple, 2, Laptop, and 999, the output should be:

true
false
Apple

The Apple (price 2) is less than the Laptop (price 999), so the first comparison is true. The Apple is not greater than the Laptop, so the second is false. And since Apple has the lower price, it's printed as the cheaper product.

Cheat sheet

The __lt metamethod handles the "less than" (<code><) operator for custom objects.

When Lua evaluates a < b, it looks for __lt in the metatable. This metamethod should return true or false based on your comparison logic.

local Item = {}
Item.__index = Item

function Item:new(name, price)
    local obj = {name = name, price = price}
    setmetatable(obj, Item)
    return obj
end

function Item.__lt(a, b)
    return a.price < b.price
end

With __lt defined, you can compare items directly:

local apple = Item:new("Apple", 2)
local laptop = Item:new("Laptop", 999)

print(apple < laptop)   -- Output: true
print(laptop < apple)   -- Output: false

Lua automatically derives the operator from __lt. When you write a > b, Lua evaluates b < a instead. Defining just __lt gives you both comparison directions.

Try it yourself

-- Require the Product module
local Product = require('Product')

-- Read inputs
local name1 = io.read()
local price1 = tonumber(io.read())
local name2 = io.read()
local price2 = tonumber(io.read())

-- TODO: Create two Product instances using Product:new()

-- TODO: Compare products using < operator and print true/false

-- TODO: Compare products using > operator and print true/false

-- TODO: Print the name of the cheaper product
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming