How to sort a table while keeping the index in mind?

I have these pets, and they are stored as such:

local PetID = HttpService:GenerateGUID(false)
	Data.OwnedPets[PetID] = {
		Name = petName,
		PetStrength = RNG:NextInteger(PetInfo.MinStrength, PetInfo.MaxStrength),
		Equipped = false,
		ModelName = PetInfo.ModelName,
		Type = PetInfo.Type
	}

and so they have their id as the index (so it’s easy to find them/display them, etc.) However, I wanna be able to sort them via their strength, which I do as such.

for id, data in pairs(Data.OwnedPets) do
		table.insert(AllPets, data)
	end
	
	table.sort(AllPets,
		function(a, b)
			return a.PetStrength > b.PetStrength
		end
	)
	
	for i, v in ipairs(AllPets) do
		
	end

But problem is, how can I then get their ids printed in the bottom for loop? Printing i would just print 1, 2, 3, etc.

PLEASE DO NOT SUGGEST PUTTING THE ID IN THE TABLE!! THE ID IS BEING USED AS THE INDEX FOR A REASON AND I WILL NOT MAKE ANY COMPRISES TO THIS!!

1 Like

Technically you can get the id by looping through the old table and searching for an exact match with the chosen pet(same Name, PetStrength, ModelName, Type), then return the index at which it was found.

What’s the point not putting the ID in the table? You’ve just made an array of tables with only number indexes. That is unless you want it to be unoptimized.

I use the id as the index for a reason. Otherwise I’d have to constantly loop through the table to find the specific id. Having it as the index is necessary. There’s no other options

You’ve literally removed its ID when you put it into that array though?

Yes because you can’t sort the table otherwise :man_facepalming:

Exactly so why are you trying to keep the index in mind without putting it in the table?

Because I need to know what pet has the highest strength. Without the ID I can’t do that

If you put the “ID” inside the table then you will have the “ID”.

:man_facepalming: you clearly don’t know what I am asking. Please read my question again

I use the ID as the index for a reason. End of story. There’s other areas of code that rely on that ID being the index. If I just put the id in the table, it’d break my entire codes structure and remove all pets players have already unlocked. I have remove it when I put it in the array, as there is no way to sort a table if it has indexs.

Ah yes so you think it’s efficient to strip the table from its index and get it back??

Anyways how would it break your code structure? You’re literally just putting the ID in the table. You’re not removing the index???

Sort a table of ids by pet strength. Then you can use the ordered ids to index back into your owned pets lookup keyed by id.

local pets = Data.OwnedPets
local ids = {}
for id in pairs(pets) do
    table.insert(ids, id)
end
table.sort(ids, function(a, b)
    -- don't forget tie-breaker logic when the strengths are equal
    -- e.g. compare their ids as a fallback
    return pets[a].PetStrength > pets[b].PetStrength
end)
2 Likes