Hello everyone
a Lua table is written as follows:
[CODE]
inv["pistol"]={title="Pistol ammo",desc="Ammo for pistols and SMGs",model="Items/BoxSRounds.mdl",price=200,bullets=60,OnUse=function(ply,id) ply:useAmmo(id) end,from="ammo",amount=5}
inv["smg1"]={title="Rifle ammo",desc="Ammo for automatic rifles",model="Items/BoxMRounds.mdl",1,price=250,bullets=60,OnUse=function(ply,id) ply:useAmmo(id) end,from="ammo",amount=5}
inv["Buckshot"]={title="Shotgun ammo",desc="Ammo for shotguns",model="Items/BoxBuckshot.mdl",1,price=250,bullets=24,OnUse=function(ply,id) ply:useAmmo(id) end,from="ammo",amount=5}
inv["357"]={title="Sniper ammo",desc="Ammo for sniper rifles",model="Items/357ammo.mdl",1,price=300,bullets=24,OnUse=function(ply,id) ply:useAmmo(id) end,from="ammo",amount=5}
inv["XBowBolt"]={title="Tranqualizer ammo",desc="Ammo for tranqualizer rifle",model="Items/CrossbowRounds.mdl",price=500,bullets=8,OnUse=function(ply,id) ply:useAmmo(id) end,from="ammo",amount=5}
[/CODE]
but it is displayed in a wrong manner:
1. Sniper
2. Shotgun
3. Tranqualizer
4. Pistol
5. Rifle
function table.sortByMemmber () and table.sort () or to have failed, is it possible to sort a table with string keys?
I need sort by price
I think they need to be numbers. I've come across that issue before, and I ended up having one structure, and then taking part of that structure and putting it into a standard table with number indexes just to sort it.
Acecool is right. You can only sort a table with numeric indexes.
You could workaround it like this, but I doubt it's required in this situation.
[lua]
inv = setmetatable({}, {
__index = function(self, key)
for k, v in pairs(self) do
if (v.key == key) then
return self[k];
end;
end;
return nil; -- string key doesn't exist
end,
__newindex = function(self, key, value)
if (type(key) == "string" and type(value) == "table") then
table.insert(self, value);
self[#self].key = key;
end;
end
});
inv["stringIndex"] = { price = 200, name = "cheapest" };
inv["anotherStringIndex"] = { price = 500, name = "middle price" };
inv["stringIndexThird"] = { price = 700, name = "most expensive" };
table.sort(inv, function(a, b) return a.price > b.price end);
for k, v in pairs(inv) do print(k, v.name, v.price) end
[/lua]
Output:
[code]
1 most expensive 700
2 middle price 500
3 cheapest 200
[/code]
[QUOTE=Khub;42390891]Acecool is right. You can only sort a table with numeric indexes.
You could workaround it like this, but I doubt it's required in this situation.
[lua]
inv = setmetatable({}, {
__index = function(self, key)
for k, v in pairs(self) do
if (v.key == key) then
return self[k];
end;
end;
return nil; -- string key doesn't exist
end,
__newindex = function(self, key, value)
if (type(key) == "string" and type(value) == "table") then
table.insert(self, value);
self[#self].key = key;
end;
end
});
inv["stringIndex"] = { price = 200, name = "cheapest" };
inv["anotherStringIndex"] = { price = 500, name = "middle price" };
inv["stringIndexThird"] = { price = 700, name = "most expensive" };
table.sort(inv, function(a, b) return a.price > b.price end);
for k, v in pairs(inv) do print(k, v.name, v.price) end
[/lua]
Output:
[code]
1 most expensive 700
2 middle price 500
3 cheapest 200
[/code][/QUOTE]
Oh mijn god
---------------------
ok, I'll do an additional table with indexes and sort through it
Sorry, you need to Log In to post a reply to this thread.