Sorting algorithm help

I wrote a basic (inefficient) sorting algorithm in python, and although there are clearly better (and well documented) methods for sorting, I’m not sure why the method I used doesn’t work. I’m sorting through a single table twice using two for loops. The table contains multiple other tables of similar structure. The function takes a parameter ‘n’ when called, which is the index of the value being compared in the nested tables. The code first checks if the element is a string. If it is a string then the index of the first element of that string in a separate reference table is compared with the first letter of the second for loop’s value string. If the element is a number then the two values from both for loops are compared directly. In either case a variable ‘index’ is increased in value by 1, depending on whether the compared value is higher or not.

``````local function sort_tab(tab, n)
local new_tab = tab
for _, a in pairs(new_tab) do
local index = 1
for i, b in pairs(new_tab) do
if type(a) == "string" then
if table.find(order_tab, a[n]:sub(1,1)) > table.find(order_tab, b[n]:sub(1,1)) then
index = index + 1
elseif table.find(order_tab, a[n]:sub(1,1)) == table.find(order_tab, b[n]:sub(1,1)) then
if table.find(order_tab, a[n]:sub(2,2)) > table.find(order_tab, b[n]:sub(2,2)) then
index = index + 1
end
end
else
if a[n] > b[n] then
index = index + 1
end
end
if i == (#new_tab-1) then
new_tab[_], new_tab[index] = new_tab[index], new_tab[_]
end
end
end
return new_tab
end
``````

Bear in mind that this was originally written in Python, which has many methods and functions which are unavailable in lua (and vice-versa). I also don’t really care about efficiency, so this is essentially pseudo-code for people to assess the logic being used.

I can post the original python if someone wants.

Can you post the original in python?

I meant to return new_tab, but the logic is the same.