Lua table out of order

Hello, I am fairly new to C++ and I have a function that for some reason is mixing up the order of the table that I make in Lua.

Here is the function in C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int32_t LuaInterface::luaDoPlayerSendChannels(lua_State* L) // player open channel changes
{
        //doPlayerSendChannels(cid[, list])
        ChannelsList channels;
        uint32_t params = lua_gettop(L);
        if(params > 1)
        {
                if(!lua_istable(L, -1))
                {
                        errorEx("Channel list is not a table");
                        lua_pushboolean(L, false);
                        return 1;
                }

                lua_pushnil(L);
                while(lua_next(L, -2))
                {
                        std::cout << "\nTest: " << (uint16_t)lua_tonumber(L, -2) << " - And: " << lua_tostring(L, -1); // this is what we are looking at for this test
                        channels.push_back(std::make_pair((uint16_t)lua_tonumber(L, -2), lua_tostring(L, -1)));
                        lua_pop(L, 1);
                }
 
                lua_pop(L, 1);
        }
 
        ScriptEnviroment* env = getEnv();
        if(Player* player = env->getPlayerByUID(popNumber(L)))
        {
                if(params < 2)
                        channels = g_chat.getChannelList(player);
 
                player->sendChannelsDialog(channels);
                lua_pushboolean(L, true);
                return 1;
        }
 
        errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
}


And this is what the Lua looks like:
1
2
3
4
5
6
7
8
9
10
local testTable = {
	[344] = "first",
	[365] = "second",
	[376] = "third",
	[398] = "fourth",
	[401] = "fifth"
}
function test(cid)
	doPlayerSendChannels(cid, testTable)
end


And it prints this:
1
2
3
4
5
Test: 376 - And: third
Test: 398 - And: fourth
Test: 344 - And: first
Test: 401 - And: fifth
Test: 365 - And: second


As I said I am new to C++ and I am at my wits end on this one. I was thinking maybe I could sort it somehow in C++ but I have no idea how. Also another person suggested using a table in Lua like this may hold the order:
1
2
3
4
5
6
7
local testTable = {
	{344, "first"},
	{365, "second"},
	{376, "third"},
	{398, "fourth"},
	{401, "fifth"}
}

The problem with that solution is I don't know how to make the function work with a table that looks like that, if I try to use it now I get the error:
1
2
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
Two things: A Lua table is almost certainly a dictionary (hash, unordered_map), so the order is dependent on the hash of the key and the buckets, not the key itself. Second, you cannot do this std::string foo(NULL);, which is what that exception is complaining about. This question is best asked on a Lua list, not a "General C++ Forum" such as this.
Topic archived. No new replies allowed.