In 0.1.1d there's no table.sort function. Intended?
You're right, it looks like table.sort isn't available in PICO-8 Lua, but you can implement something similar yourself. Here's an example:
function sort(t) for i=2,#t do local k,j=t[i],i-1 while j>0 and t[j]>k do t[j+1]=t[j] j-=1 end t[j+1]=k end end t={5,3,8,1,4} sort(t) for i=1,#t do print(t[i]) end |
edit: Confirmed this works in Picotron as well.
table.sort is not available in Picotron
There are two alternatives that I know of, either using a custom sorting function:
abledbody shared their quicksort implementation here: https://www.lexaloffle.com/bbs/?tid=142305
And kimiyoribaka modified it so you can use it like table.sort in the comments here: https://www.lexaloffle.com/bbs/?tid=144442
Or:
Using userdata:sort
This can be a bit fiddly & might not be a good choice for every case - checkout demos/carpet.p64 for an example
Thanks for the links. I wonder why table.sort has been excluded, or if it's just not implemented yet.
[Please log in to post a comment]