pesde goatsoft / lucy

useSort

Lucy.useSort sorts any list by one or more keys (a grid of cards, a shop list). useTable uses it under the surface.

Unlike useTable, which wraps each entry into a table row with row:value(), useSort returns your original items sorted in items().

Usage

local sortModel = Lucy.useSort(options)

Options

useSort takes the same options as useTable, without rowId or manual:

OptionTypeDefaultWhat it does
dataarray | Ref | () -> arrayrequiredItems to sort
columnsarrayrequiredSort keys and compare functions
sortingtable | Ref | falsefalseStarting sort
multibooleanfalseAllows secondary columns as tiebreakers
sortablebooleantruefalse turns sorting off
removablebooleantrueA third toggle clears that key from the sort
descFirstbooleanfalseFirst toggle sorts descending

Returns

Returns the same methods as useTable, plus:

NameTypeWhat it is
items()() -> arrayYour items in sorted order
sort(list)(list: array) -> arraySorts another list with the same rules

Example: emote shop sorting

Sort emote cards by rarity, style and name using SortButton:

local RARITY_RANKS = { Common = 1, Rare = 2, Epic = 3, Legendary = 4 }

local ownedEmotes = Lucy.ref({
    { name = "Moonwalk", rarity = "Epic", style = "Retro" },
    { name = "Robot", rarity = "Common", style = "Street" },
    { name = "Shuffle", rarity = "Legendary", style = "Club" },
})

local sortedEmotes = Lucy.useSort({
    data = ownedEmotes,
    columns = {
        { key = "name" },
        { key = "style" },
        {
            id = "rarity",
            get = function(emote) return RARITY_RANKS[emote.rarity] end,
            descFirst = true,
        },
    },
    sorting = { { id = "rarity", desc = true } },
    multi = true,
})

-- Render the sorted items with Lucy.each
Lucy.each(sortedEmotes.items, function(emote)
    return EmoteCard(emote)
end)

-- Clicking a name header adds "name" as the secondary tiebreaker:
sortedEmotes.toggle("name")