Herobrine2Nether
Herobrine2Nether

Reputation: 51

How to sort inner tables by value?

So, as the title says, I would like to sort a table of tables in Lua. One such example nested table is below.

tabl = {2.0={amount=281.0, meta=0.0, displayName=Dirt, name=minecraft:dirt}, 3.0={amount=190103.0, meta=0.0, displayName=Cobblestone, name=minecraft:cobblestone}, ...}

I would like to go through and return a table of the top ten tabl[*]['amount'] listed with their respective tabl[*]['displayName'] * being a wildcard for tabl[1.0] through tabl[max.0]

A finished table should look something like:

sorted = {1={displayName=Cobblestone, amount=190103}, 2={displayName=Dirt, amount=281}, ...}

I hope this makes sense to all out there.

Link to full nested table: Full Piece FYI: I am not in control of how the table is returned to me; I got them from the function listItems() in this API.

Upvotes: 1

Views: 327

Answers (2)

Herobrine2Nether
Herobrine2Nether

Reputation: 51

So, I worked at it for a while, and thanks to community answers I came up with this piece:

bridge = peripheral.wrap("left")
items = bridge.listItems()

sorted = {}

for i, last in next, items do
  sorted[i] = {}
  sorted[i]["displayName"] = items[i]["displayName"]
  sorted[i]["amount"] = items[i]["amount"]
end

table.sort(sorted, function(a,b) return a.amount > b.amount end)

for i = 1, 10 do
  print(i .. ": " .. sorted[i].displayName .. ": " .. sorted[i].amount)
end

It returned the top 10 inventories:

1: Cobblestone: 202924
2: Gunpowder: 1382
3: Flint: 1375
4: Oak Sapling: 1099
5: Arrow: 966
6: Bone Meal: 946
7: Sky Stone Dust: 808
8: Certus Quartz Dust: 726
9: Rotten Flesh: 627
10: Coal: 618

Upvotes: 0

warspyking
warspyking

Reputation: 3113

First of all, your arrays aren't syntactically correct. It should be more like:

local people = {
    {Name="Alice",Score=10},
    {Name="Bob",Score=3},
    {Name="Charlie",Score=17}
}

Secondly, the table.sort function should do the job. In my particular example it would look like this:

table.sort(people, function(a,b) return a.Score > b.Score end)

And finally, to get the top N just iterate:

for i = 1,N do
    print(people[i].Name, people[i].Score)
end

Upvotes: 3

Related Questions