How do I compare two tables with the same values but different order? [SOLVED]

Currently, I’m making an inventory system, and I want to compare two saved tables (containing items the player had BEFORE and AFTER an update).

For context, the script updates whenever the backpack is changed or the function is called upon manually. However, there are some items that don’t show up in the inventory (ex: flashlight; it has a different gui for it). So, sometimes, when the flashlight is equipped, the whole inventory system is updated, even though it would be the exact same as before (it can lead to a flash of the items on the gui).

Because I want this to be more automated (especially since I want to add some more guis for special items), I need a way to compare two tables to check if they just have the same items and amount of items. I want to make it so it doesn’t compare the order that the items are in (it could be saved in the opposite direction; one could be saved from the backpack first and then the player model and vice versa), just the amount and type of the items in the table. How would I do this?

I thought about using loops to save items in new tables, but I don’t actually know how I could save that information without needing a function to then compare the new tables.

I had an idea where I could save objects in their own tables, but then I would have to make new tables on the spot and somehow save all of those new tables and then loop through them, but wouldn’t I need tables of tables in that case? It’s just a little confusing, and I don’t know where to begin. Help is appreciated!

I saw a post similar to this that would’ve provided the answer, but their solution wasn’t code and instead added to what they had before, so I couldn’t use it. I saw somewhere that I could use table.sort to possibly help, but I don’t understand how.

If you need anything, ask below! :slight_smile:

table.sort() would be your solution.

  • By default, table.sort() will sort numbers by ascending order. In this case, you need something different.
  • table.sort() also allows you to pass a sorting function.

So, you need something to sort by here. You could give each item some sort of number value, or sort by name or something like that.

  • The bottom line is each item needs a unique identifier no other item has, but other items of the same type will share.
    • So i.e. flashlight could be 1, and every time the table is sorted flashlight will be 1

Depending on your chosen method you’ll need a different sorting function. If you wanted to sort by descending order for example,
Our table contains the following

{
 a = {
  data = "whatever",
  id = 2
}, 
 b = {
  data = "whatever", 
  id = 3
}
  • table.sort(table, function(one,two) return one.id > two.id end)
  • This would return the result: {b, a}

If you wanted to sort alphabetically, you could also use >/< on .Name properties of items, or in some cases they recommend using string.lower() and then using the same.

If you need to know the length of the table, you can simply run #table on each table, which returns the length

1 Like

Assuming there are no duplicate entries, you can check if:

  1. The length of the tables are the same
  2. All elements in table A can be found in table B

Here is a function that should do this:

local function IsEqual(A, B)
	if #A ~= #B then
		return false
	end
	for _, item in A do
		if not table.find(B, item) then
			return false
		end
	end
	return true
end
1 Like

There would be a duplicate issue, but because the indicator that says you have duplicates is just text, it doesn’t flicker or change the image (I use viewport frames for the item images), so I can just put the amount of objects’ text outside the IsEqual if statement for it to work. Thanks!

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