Jump to content




How to use table.sort?


  • You cannot reply to this topic
5 replies to this topic

#1 Agent Silence

  • Members
  • 319 posts
  • Location[string "FindMe"]:23143: bad argument #1 to 'returnPos' (vector expected, got nil)

Posted 17 July 2014 - 12: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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2014 - 12: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 theoriginalbit, 17 July 2014 - 12:15 PM.
damn emoticon auto-formatting


#3 Agent Silence

  • Members
  • 319 posts
  • Location[string "FindMe"]:23143: bad argument #1 to 'returnPos' (vector expected, got nil)

Posted 17 July 2014 - 12:21 PM

View Posttheoriginalbit, on 17 July 2014 - 12:15 PM, said:

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

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2014 - 12: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.

#5 Agent Silence

  • Members
  • 319 posts
  • Location[string "FindMe"]:23143: bad argument #1 to 'returnPos' (vector expected, got nil)

Posted 17 July 2014 - 12:33 PM

View Posttheoriginalbit, on 17 July 2014 - 12:25 PM, said:

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

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2014 - 12: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 by theoriginalbit, 17 July 2014 - 12:38 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users