newTableview sorting issue (hopefully simple)

Hello Everyone,

So I have a list of data that shows in a tableview on screen (all works great) and I have created a button to sort the information in the list as either original, ascending or descending.   Pretty straight forward.

It works great with one exception.  original does not work. The list shows initially in its original order, clicking the sort button re-orders it to ascending. clicking it again orders it to descending.  Clicking a third time should bring it back to the original order and start the process again, however the list remains in the descended order.  Clicking a fourth time correctly reorders the list to ascending.

here is the code for the sort function

local function sort\_listrecs(data) local temp\_table = data storyboard.sorting = storyboard.sorting + 1 if storyboard.sorting \> 2 then storyboard.sorting = 0 end if storyboard.sorting == 0 then temp\_table = tempdata end if storyboard.sorting == 1 then table.sort(temp\_table, function(a,b) return a.company \< b.company end) end if storyboard.sorting == 2 then table.sort(temp\_table, function(a,b) return a.company \> b.company end) end listrecs = {} for x = 1, #temp\_table do listrecs[x] = {} listrecs[x].company = temp\_table[x].company local temp = temp\_table[x].details listrecs[x].offer = temp.offer listrecs[x].address = temp.address listrecs[x].text = temp.text listrecs[x].categories = temp.categories listrecs[x].distance = temp.distance end list:deleteAllRows() for x = 1, #listrecs do list:insertRow { rowHeight = 48, } end end local function button\_sort\_release() sort\_listrecs(tempdata) return true end

Some of the code is my attempt to resolve the issue, like the use of temp_data instead of data but to no effect.  I initially had no action taken on the data when storyboard.sorting was equal to 0 but this started so I tried to have it directly reassign the original data into the temporary table, alas still to no use.

So to summarize,  sorting works without error but returning to the original order fails. the table tempdata is written to only once and not edited again after then, so the original order should remain intact.  

Ha I posted too soon.

Even though I was assigning the table to new variables, they were all just referring to the 1 table of information anyway, so I did this to resolve :

local function sort\_listrecs(data) local temp\_table = {} for i = 1, #data do table.insert(temp\_table, data[i]) end storyboard.sorting = storyboard.sorting + 1 if storyboard.sorting \> 2 then storyboard.sorting = 0 end if storyboard.sorting == 0 then temp\_table = tempdata end if storyboard.sorting == 1 then table.sort(temp\_table, function(a,b) return a.company \< b.company end) end if storyboard.sorting == 2 then table.sort(temp\_table, function(a,b) return a.company \> b.company end) end --table.sort(temp\_table) listrecs = {} for x = 1, #temp\_table do listrecs[x] = {} listrecs[x].company = temp\_table[x].company local temp = temp\_table[x].details listrecs[x].offer = temp.offer listrecs[x].address = temp.address listrecs[x].text = temp.text listrecs[x].categories = temp.categories listrecs[x].distance = temp.distance end list:deleteAllRows() for x = 1, #listrecs do list:insertRow { rowHeight = 48, } end end

Ha I posted too soon.

Even though I was assigning the table to new variables, they were all just referring to the 1 table of information anyway, so I did this to resolve :

local function sort\_listrecs(data) local temp\_table = {} for i = 1, #data do table.insert(temp\_table, data[i]) end storyboard.sorting = storyboard.sorting + 1 if storyboard.sorting \> 2 then storyboard.sorting = 0 end if storyboard.sorting == 0 then temp\_table = tempdata end if storyboard.sorting == 1 then table.sort(temp\_table, function(a,b) return a.company \< b.company end) end if storyboard.sorting == 2 then table.sort(temp\_table, function(a,b) return a.company \> b.company end) end --table.sort(temp\_table) listrecs = {} for x = 1, #temp\_table do listrecs[x] = {} listrecs[x].company = temp\_table[x].company local temp = temp\_table[x].details listrecs[x].offer = temp.offer listrecs[x].address = temp.address listrecs[x].text = temp.text listrecs[x].categories = temp.categories listrecs[x].distance = temp.distance end list:deleteAllRows() for x = 1, #listrecs do list:insertRow { rowHeight = 48, } end end