How to sort things by recency

I’m making some different filters for a table in a project I’m working on and would like the player to be able to sort the items within by how recent they acquired them. How would I go about this?

I have other methods of sorting set up like by rarity or damage (I am using layout order to sort things) but am not sure how to set up a recency filter.

Here’s some of the code showing how I have filters working right now, as I’m not sure the method I’m using would allow for sorting by recency:

if method == "Rarity" then
		currentFilter = "Rarity"
		for i, cardIcon in pairs(cardsScroll:GetChildren()) do
			if cardIcon:IsA("ImageButton") then
				cardIcon.Visible = true
				local cardRarity = CardInfo.Cards[cardIcon.Name].RarityForFilter
				cardIcon.LayoutOrder = -cardRarity
			end
		end
	elseif method == "Amount" then
		currentFilter = "Amount"
		for i, cardIcon in pairs(cardsScroll:GetChildren()) do
			if cardIcon:IsA("ImageButton") then
				cardIcon.Visible = true
				local cardAmount = CardInfo.Cards[cardIcon.Name].CardAmount
				cardIcon.LayoutOrder = -cardAmount
			end
		end
-- Just more similar filters past this

Store a time as part of the card’s data for the player (i.e. in a player’s save data, each card should have an acquisition time). You can then use those timestamps as your sorting value.

1 Like

Thanks! Very helpful.

Leaving what I did in case it would be helpful to anyone in the future:

if pulledCard.HasACopy ~= true then
	pulledCard.HasACopy = true
	pulledCard.TimeAcquired = os.time()
end