Sorting a table based of string values

I’ve looked all over the dev forum and cannot find a solution. I’m trying to sort the values I have in a table by rarity. The table holds string values. The string values each have the rarity set to their value. For ex: “Common”, “Uncommon,” “Rare”. How would I sort this table so that all the commons are first and then so on?

You mean as the script is adding to the table?

You could compare the index of the rarity of the item

local Rarities = {
	"Common",
	"Uncommon",
	"Rare",
	"Epic",
	"Legendary"
}

local Items = {
	"Common",
	"Rare",
	"Rare",
	"Rare",
	"Epic",
	"Legendary",
	"Uncommon",
	"Rare",
	"Legendary",
	"Common",
	"Common"
}

table.sort(Items, function(A, B)
	return table.find(Rarities, A) < table.find(Rarities, B)
end)

for i, v in ipairs(Items) do
	print(i, v)
end

image

2 Likes

Could we see exactly what table you’re working with or a repro sample? It’s not immediately clear what kind of table you’re working with and as a visual person, having something to work off of to help explain what table you’re working with would better help some be able to provide a solution.

I’m a little confused by the explanation you’ve presented, specifically. You say your table contains string values and the strings are the rarity’s value… if I have this correct? Does your table then look something like this?:

{ Common, Common, Common, Uncommon, Rare }

If not, how does your table look like?

My table is just a bunch of string values, but those values contain the rarity in them.

Could you please show an example? This is the very part I don’t understand: what do you mean by this? Does your table look like the one I posted above or?

image

Alright, I see now. I had to reread the code again after making a post because this just shows how you’re adding items to the table rather than the composition (I was asking for the table’s composition), but this works if I can stitch some of my own knowledge here.

Given the way your function is written, it seems like you’re creating a table of StringValue objects. Your table is effectively just one where all the elements are references to objects in the DataModel then. Out of curiosity, why’ve you chosen to do it this way instead of inserting their values to the table?

The composition of your table looks something like this:

{ StringValue, StringValue, StringValue }

If you inserted their values instead, you’d have a table of their values instead of objects. For example: if the values of your StringValue were “first”, “second” and “third” respectively:

{ first, second, third }

What exactly is going to be your criteria for sorting then? What is the name and value of these StringValues and how do you expect to be able to sort them by rarity? This is the kind of thing that confuses me: because you still haven’t mentioned what your table composition is or provided much detail, I have to keep probing around to get you to give those needed details to be able to accurately help you.

You need to provide much more detailed information for this thread.

Using hell_ish’s code from earlier, I was able to make sort it

local Rarities = {
	"Common",
	"Uncommon",
	"Rare",
	"Epic",
	"Legendary"
if items ~= nil then
		table.sort(items, function(A, B)
			return table.find(Rarities, A.Value) < table.find(Rarities, B.Value)
		end)
	end