• Table sorting
    3 replies, posted
Hey guys, this is more of an pure Lua question, anyhow; I want to order tables by a "priority" value inside of them They look something like this: [CODE] tab1{ callback=function,priority=421 }, tab2{ callback=function,priority=24 }, tab3{ callback=function,priority=1532 } [/CODE] So basically I want it in this order: tab2, tab1, tab3 Now, my problem is the following: [CODE]local sorted = {tab1={pri=1},tab2={pri=3},tab3={pri=63}} table.sort(sorted, function(a,b) print("Comparing") return a.pri > b.pri end)[/CODE] As there are tables in this table it doesnt seem to be called at all, not even "Comparing" will be printed in console However if i set the sorted var to [CODE]local sorted = {412,421,4,12}[/CODE] it does print "Comparing" and well, sorts. Help me out please :v
table.sort only works on integer indexed tables.
You should rewrite your table so that keys are the priority: [CODE]tab = { [421] = function, [24] = function, [1532] = function }[/CODE]
If you don't name your keys, you can use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/table/SortByMember]table.SortByMember[/url].
Sorry, you need to Log In to post a reply to this thread.