Sorting a table with multiple key values

-- This is an example Lua code block If you say so

What I am attempting to achieve is sorting a table with multiple values in a certain order.

The Order I am attempting to achieve.
Rarity > Level > Name

So, a “Higher Tier” Rarity would be placed higher than a “Lower Tier” rarity.

In an example, a rare would be higher than a Common.

I then want to sort by level.
So for an example
A common peasant lvl 15 would be placed before a common peasant lvl 7 or a common noob lvl 5

Lastly, I would like to finalize the sorting by alphabetical. So things with the same rarity and lvl would then be prioritized by alphabetical. So a peasant lvl 10 would be behind a noob lvl 10.

Currently, I achieved the rarity and level sorting, but I can’t seem to get the alphabetical search as well.
2c3a081e6ca1a5324990fb6a6b3e74ae
you can see here at the bottom it goes Dummy > Noob > Dummy >Noob, or in the middle it has the Human behind Peasant

And I for some reason just can’t wrap my head around adding in the last part. I have tried things like.

A.Name:lower() < B.Name:lower()
A.Name < B.Name

Here is how the code is setup Currently

table.sort(Inv, function(A, B)
		if A:GetAttribute("Rarity") ~= B:GetAttribute("Rarity") then
			return table.find(Rarities, A:GetAttribute("Rarity")) < table.find(Rarities, B:GetAttribute("Rarity"))
		elseif A:GetAttribute("Level") >  B:GetAttribute("Level") then
			return A:GetAttribute("Level") > B:GetAttribute("Level")
			else
			return A.Name < B.Name
			end
		end)

The only other solution I could think of was to assign a number to the first letter and attempt to sort like that, but that seems like a lot of extra steps for something that seems to be simple. I just don’t know why I can’t wrap my head around it lol anyway

Yes, as that is what I am attempting to do. If you look at my posted screenshot, it does just that.

It Puts the Rarity in Order
Legendary → Rare → UnCommon → Common

It then puts the higher levels first
So Dummy Level 4 → Dummy Level 2 which is = to Noob Level 2

And it does both of those without hassle, it’s just sorting them by alphabetical.

Even comparing to what yours does. It is the same problem.


The only difference is my sorts, the rarity
Legendary → Rare → UnCommon → Common where your is doing
UnCommon → Rare → Legendary → Common

but thats just due to the wrong use of > and im comparing myn to a table while yours is default alphabetical
C → L → R → U

Anyway’s I just figured out part of my issue which was me thinking the name was the object when i was storing it as a value but not im still having one slight hiccup

So updated code


function SortInventory.SortINV(Inv)
	table.sort(Inv, function(A, B)
		if A:GetAttribute("Rarity") ~= B:GetAttribute("Rarity") then
			return table.find(Rarities, A:GetAttribute("Rarity")) < table.find(Rarities, B:GetAttribute("Rarity"))
		elseif A:GetAttribute("Level") >  B:GetAttribute("Level") then
			return A:GetAttribute("Level") > B:GetAttribute("Level")
		else
			return A.Value < B.Value
		end
	end)

Updated Result
aad892b2a3463126d2bde1898c5dab74
The new issue is number 4
Human should be under peasant Level 1

Okay so I have figured it out and here is the code for anyone who is trying to do something similar.

table.sort(Inv, function(A, B)
		if A:GetAttribute("Rarity") ~= B:GetAttribute("Rarity") then
			return table.find(Rarities, A:GetAttribute("Rarity")) < table.find(Rarities, B:GetAttribute("Rarity"))
		elseif A:GetAttribute("Level") ~= B:GetAttribute("Level") then
			return A:GetAttribute("Level") > B:GetAttribute("Level")
		else
			return A.Value < B.Value
		end
	end)

Result
5ec82ab0f1a1f6e850a818f1e0cd41e1

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.