Returning A Sorted List's Index in Lua

微笑、不失礼 提交于 2019-12-11 11:13:04

问题


I access object properties with an index number

object = {}
object.y = {60,20,40}
object.g = {box1,box2,box3} -- graphic
object.c = {false,false,false} -- collision
-- object.y[2] is 20 and its graphic is box2
-- sorted by y location, index should be, object.sort = {2,3,1}

I know table.sort sorts a list, but how can I sort the y list that returns index for the purpose of drawing each object in-front depending on the y location.

Maybe the quicksort function can be edited, I don't understand it. http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Lua

https://github.com/mirven/lua_snippets/blob/master/lua/quicksort.lua

Is this possible?


回答1:


This should work:

local temp = {}
local values = object.y
-- filling temp with all indexes
for i=1,#values do
    temp[i] = i
end
-- sorting the indexes, using object.y as comparison
table.sort(temp,function(a,b)
    return values[a] < values[b]
end)
-- sorting is done here, have fun with it
object.sort = temp

temp will be {2,3,1} when using this code combined with yours.




回答2:


Do not store the data as you're currently doing. Use something like:

object = {
  {
    y = 60,
    g = box1,
    c = false,
  },
  {
    y = 20,
    g = box2,
    c = false,
  },
  {
    y = 40,
    g = box3,
    c = false,
  },
}

and then use the following callback function in table.sort:

function CustomSort(L, R)
  return L.y > R.y
end

as shown below:

table.sort(object, CustomSort)



回答3:


@EinsteinK @hjpotter92 : Thank you

RESULT: This is the final version of the answers I received. My question is solved.

Use sortIndex(object) to get sorted list in object.sort . Update sort after objects move.

box1 = love.graphics.newImage("tile1.png")
box2 = love.graphics.newImage("tile2.png")
box3 = love.graphics.newImage("tile3.png")
hero = love.graphics.newImage("hero.png")
object = {
    {   x = 200,    y =  50,    g = box1 },
    {   x =  50,    y = 100,    g = box2 },
    {   x = 150,    y = 200,    g = box3 },
    {   x =   0,    y =   0,    g = hero }
}
function sortIndex(item)
--  Sort id, using item values
    local function sortY(a,b)
        return item[a].y < item[b].y
    end
    --------------------------------
    local i
    local id = {}       -- id list
    for i = 1, #item do -- Fill id list
        id[i] = i
    end
--  print( unpack(id) ) -- Check before
    table.sort(id,sortY)-- Sort list
--  print( unpack(id) ) -- Check after
    item.sort = id      -- List added to object.sort
end

sortIndex(object) -- print( unpack(object.sort) ) -- Check sorted id's

function drawObject()
    local i,v, g,x,y
    for i = 1, #object do
        v = object.sort[i] -- Draw in order
        x = object[v].x
        y = object[v].y
        g = object[v].g
        love.graphics.draw(g,x,y)
    end
end


来源:https://stackoverflow.com/questions/35309319/returning-a-sorted-lists-index-in-lua

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!