Reorder table element (SOLVED)

I have

global_.game_data.turn.step = {
[22] = 0,
[3] = 0,
[2] = 1,
[4] = 0,
}

i want

global_.game_data.turn.step = {
[2] = 1,
[4] = 0,
[22] = 0,
[3] = 0,
}

or I have

global_.game_data.turn.step = {
[22] = 0,
[3] = 0,
[2] = 0,
[4] = 1,
}

i want

global_.game_data.turn.step = {
[4] = 1,
[22] = 0,
[3] = 0,
[2] = 0,
}

or I have

global_.game_data.turn.step = {
[4] = 0,
[22] = 1,
[3] = 0,
[2] = 0,
}

i want

global_.game_data.turn.step = {
[22] = 1,
[3] = 0,
[2] = 0,
[4] = 0,
}

just move element with no zero to top and circle reorder with zero
or this imposible?

This is all Lua related.

Printing (i.e. iterating on the keys of) a Lua table isn’t guarantueed to output a sorted list of keys, as it’s a hash table, and their storage details are internal to Lua.

You could store the items as pairs instead, then it should be easier to sort them:

x = { {4,0}, {22,1}, {3,0}, {2,0} }
table.sort(x, function (a,b) return a[2] > b[2]; end)
for k,v in ipairs(x) do print(k, v[1], v[2]) end
1 Like

Many thanks ))

yes element with 1 value go to top corectly

1	22	1
2	3	0
3	4	0
4	2	0

how about other element

i need

1	22	1
2	3	0
3	2	0
4	4	0

very strange languge ((

I need this to set the Z coordinate depending on the player’s turn order

Not sure how to sort that? It feels like something is missing for that to happen?
I.e. how would you know that 3 should be sorted before 2 and 4?

However, once you do know that, you can simply modify your sort function to sort on each value at a time. E.g.:

table.sort(x, function (a,b)
                    if a[2] == b[2] then
                        return a[1] < b[1];
                    else
                        return a[2] > b[2];
                    end;
                end)

very strange languge

In which sense?
Perhaps that would help if you could specify what is missing?

in order after the element with one

i have this table and i need use in loop

x = { {4,1}, {22,0}, {3,0}, {2,0} } -- do nothing

x = { {4,0}, {22,1}, {3,0}, {2,0} } -- have
x = { {22,1}, {3,0}, {2,0} , {4,0}} -- need - just top three element  {22,1}, {3,0}, {2,0} and set {4,0} to last

x = { {4,0}, {22,0}, {3,1}, {2,0} } -- have
x = { {3,1}, {2,0}, {4,0} , {22,0}} -- need - just top two element  {3,1}, {2,0}  and set {4,0}, {22,0} to last

x = { {4,0}, {22,0}, {3,0}, {2,1} } -- have
x = { {2,1}, {4,0}, {22,0} , {3,0}} -- need - just top one element  {2,1} and set {4,0}, {22,0}, {3,0} to last

and so on in a circle to make a shift

you need something like this

local function sort(x)
	local index = 0
	for i = 1, #x do
		if x[i][2] == 1 then
			index = i
			break
		end
	end
	if index > 1 then
		local ret = {}
		for i = index, #x do
			table.insert(ret, x[i])
		end
		for i = 1, index - 1 do
			table.insert(ret, x[i])
		end
		return ret
	else
		return x
	end
end

function init(self)
	local x = { {4,0}, {22,1}, {3,0}, {2,0} }
	pprint(sort(x))
end

2 Likes

Yes )) it work

thank you very much

RIght, so I think I understand now.
So to describe the algorithm, it’d be something like:

  • Find index of new “head” of the list, store in count
  • Rotate the list left count steps

What @Chung_Xa outlined should work (you probably want to modify it a bit to detect the highest number).
But I wouldn’t call it a “sort” anymore, the common name for this operation is “rotate”.
My advice would be to split it up into two function accordingly:

local count = find_highest_index(a)
rotate_left(a, count)

no number - number is player id

And I need to sort the characters so that they are not on the same line.

active character always on top. Whoever goes next is behind him and so on

The following function maps these:

local function cycle_table(t)
  for i=1,#t do
    if t[i][2]==1 then
      local n_t={}
      for ii=1,#t do
        n_t[ii]=t[i+ii-2<#t and i+ii-1 or i+ii-1-#t]
      end
      return n_t
    end
  end
end
-- usage:
xx = cycle_table(x)

Thank you. Works. I can’t get used to random tables