This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Agent Silence's profile picture

How to use table.sort?

Started by Agent Silence, 17 July 2014 - 11:05 AM
Agent Silence #1
Posted 17 July 2014 - 01:05 PM
How do you pull up the highest/lowest "Priority" from this table using table.sort?


tasks = {dig = {priority = 100}, mine = {priority = 120, timeout = 10}}

Im really confused, please help
theoriginalbit #2
Posted 17 July 2014 - 01:15 PM
As shown on the Lua-users.org Tables Tutorial you can provide a second argument to table.sort that is a comparator function where you compare the two arguments provided to it, therefore you'd do the following


table.sort( tasks, function( a, b ) return a > b end )
Edited by
Agent Silence #3
Posted 17 July 2014 - 01:21 PM
As shown on the Lua-users.org Tables Tutorial you can provide a second argument to table.sort that is a comparator function where you compare the two arguments provided to it, therefore you'd do the following


table.sort( tasks, function( a, b ) return a > b end )
It helps a little, but how would It compare the priorities
theoriginalbit #4
Posted 17 July 2014 - 01:25 PM
well the values passed through in a and b are the table entries, so you simply just do a.priority and b.priority.
Agent Silence #5
Posted 17 July 2014 - 01:33 PM
well the values passed through in a and b are the table entries, so you simply just do a.priority and b.priority.

Well that helps alot!, Thank you!

Off Topic : Just in case you were wondering, I am making In-Game NBS using your MoarPeripherals mod
PM me if you are interested in it
theoriginalbit #6
Posted 17 July 2014 - 01:38 PM
Oh very nice. I look forward to seeing it complete :)/> though you might want to brush up on binary manipulation if you plan on actually saving out into the NBS format.
Edited on 17 July 2014 - 11:38 AM