Home > Mobile >  Lua - Help sorting table array alphabetical from value inside itself
Lua - Help sorting table array alphabetical from value inside itself

Time:12-07

I'm fairly new to lua coding, been doing it for GTAV FiveM scripts I'm also new here to sorry if I do or say explain something wrong.

I've been trying to make a script that handles the names of wheels, their id and wether or not they are already installed on the vehicle.

The issue I'm having is more OCD'ing up the table its creating. The way to grab the wheel names is by their ID's but this causes and issue where it makes duplicate NAMES, but they are actually different variants of them.

This is a snippet of the code I'm using to make the table for claritys sake:

for i = 1, (GetNumVehicleMods(vehicle, 23)  1) do

    local modName = GetLabelText(GetModTextLabel(vehicle, 23, (i-1)))

    txt = ""
    if GetVehicleMod(vehicle, 23) == (i-1) and tonumber(originalWheel) == tonumber(wheeltype) then  
        txt = "Currently Installed"
    end
        validMods[i] =
        {
            id = (i-1),
            name = modName,
            install = txt
        }
end

This...from what I believe, its making a list of the wheels by their ID for example:

validMods = {
    [1] = { id = 0, name = "Inferno", install = "Currently Installed" },
    [2] = { id = 1, name = "Deep Five", install = "" },
    [3] = { id = 2, name = "Inferno", install = "" },
    [4] = { id = 3, name = "Deep Five", install = "" }
}

this obviously is just a basic typed example, where the actual table can have up to 200 disorganised and duplicate results..but I can't for the life of me figure out how to reogranise the table to be alphabetical. Keep the contents intact, but reorganise them to be in order from the name instead to be like this?

validMods = {
    [1] = { id = 0, name = "Inferno", install = "Currently Installed" },
    [2] = { id = 2, name = "Inferno", install = "" },
    [3] = { id = 1, name = "Deep Five", install = "" },
    [4] = { id = 3, name = "Deep Five", install = "" }
}

Also, if this is possible...is it possible to add extra text on to the end of duplicate names..for example: Inferno, Inferno var2, Inferno Var3


Thanks for any help that can be given

CodePudding user response:

As you get those names in a specific order you need to sort them once the table has been populated.

To rename duplicates count how often you have encountred a name.

-- demo input
local validMods = {
    [1] = { id = 0, name = "Inferno", install = "Currently Installed" },
    [2] = { id = 1, name = "Deep Five", install = "" },
    [3] = { id = 2, name = "Inferno", install = "" },
    [4] = { id = 3, name = "Deep Five", install = "" }
}
-- count and rename
local names = {}
for i,v in ipairs(validMods) do
 if names[v.name] then
   names[v.name] = names[v.name]   1
   v.name = v.name .. " var" .. names[v.name]
 else
  names[v.name] = 1
 end
end
-- sort the table by name ascending
table.sort(validMods, function(a,b) return a.name < b.name end)

-- print sorted names
for i,v in ipairs(validMods) do print(v.name) end
  • Related