I’m trying to use table.sort for this table:
saveData[i].date
And here is an example of what the string in this table could look like:
161017
I’ve wrote this long function to format my time codes like this, I’m pretty sure it could be done way more effecient but that’s not really my question. But I’m throwing it in here because it includes my sort statement:
function functions.formatDates(event) local monthsTable = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} local backupTable = copyTable(saveData) local table = saveData local replaceTable = {} for i=1, #table do if #table[i].date == 9 then table[i].date = "0" .. table[i].date end table[i].date = table[i].date:gsub("%'", "") replaceTable[1] = table[i].date:sub(8,9) replaceTable[2] = table[i].date:sub(4,6) replaceTable[3] = table[i].date:sub(1,2) table[i].date = replaceTable[1] .. replaceTable[2] .. replaceTable[3] print("TABLE:: " .. table[i].date) if table[i].date:find(monthsTable[1]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[1], "01") elseif table[i].date:find(monthsTable[2]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[2], "02") elseif table[i].date:find(monthsTable[3]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[3], "03") elseif table[i].date:find(monthsTable[4]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[4], "04") elseif table[i].date:find(monthsTable[5]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[5], "05") elseif table[i].date:find(monthsTable[6]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[6], "06") elseif table[i].date:find(monthsTable[7]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[7], "07") elseif table[i].date:find(monthsTable[8]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[8], "08") elseif table[i].date:find(monthsTable[9]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[9], "09") elseif table[i].date:find(monthsTable[10]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[10], "10") elseif table[i].date:find(monthsTable[11]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[11], "11") elseif table[i].date:find(monthsTable[12]) ~= nil then table[i].date = table[i].date:gsub(monthsTable[12], "12") end print("Date value: " .. table[i].date) end saveData = table print("") print("") print("Filtered table: ") for i=1, #saveData do print("") print("----------------------------------------------------------------------") print("") print("Name: " .. saveData[i].name) print("Date: " .. saveData[i].date) print("Amount: " .. saveData[i].amount) print("Category: " .. saveData[i].category) print("ID: " .. saveData[i].id) print("") print("----------------------------------------------------------------------") print("") end table.sort(saveData, functions.compareByDate) saveData = copyTable(backupTable) for i=1, #saveData do print("") print("----------------------------------------------------------------------") print("") print("Name: " .. saveData[i].name) print("Date: " .. saveData[i].date) print("Amount: " .. saveData[i].amount) print("Category: " .. saveData[i].category) print("ID: " .. saveData[i].id) print("") print("----------------------------------------------------------------------") print("") end end
So basically what this does is format the original date format string to what I’ve shown above, but it also has the table.sort statement in there and that’s where I’m getting an error:
17:27:17.922 ERROR: Runtime error 17:27:17.922 D:\PayMeBack\Code\PayMeBack\home.lua:624: attempt to call field 'sort' (a nil value) 17:27:17.922 stack traceback: 17:27:17.922 D:\PayMeBack\Code\PayMeBack\home.lua:624: in function 'formatDates' 17:27:17.922 D:\PayMeBack\Code\PayMeBack\home.lua:471: in function '\_onEvent' 17:27:17.922 ?: in function '?' 17:27:17.922 ?: in function \<?:224\> 17:27:17.922 ?: in function \<?:169\>
This is how my sort function looks like:
function functions.compareByDate( a, b ) return a.date \< b.date end
I’m puzzled… The date format looks like it should be able to pass my compare function, but it doesn’t. Any suggestions?