how to sort data table in table

table: 0C783508 { [1] =\> table: 0C783508 {          [SetFv] =\> true          [pairing\_id] =\> 1        } [2] =\> table: 0C783508 {          [SetFv] =\> true          [pairing\_id] =\> 2        } [13] =\> table: 0C783508 {           [SetFv] =\> true           [pairing\_id] =\> 13         } [14] =\> table: 0C783508 {           [SetFv] =\> false           [pairing\_id] =\> 14         } [25] =\> table: 0C783508 {           [SetFv] =\> true           [pairing\_id] =\> 14         } [26] =\> table: 0C783508 {           [SetFv] =\> false           [pairing\_id] =\> 14         } [30] =\> table: 0C783508 {          [SetFv] =\> false           [pairing\_id] =\> 14         } [42] =\> table: 0C783508 {          [SetFv] =\> true           [pairing\_id] =\> 14         } [45] =\> table: 0C783508 {           [SetFv] =\> true           [pairing\_id] =\> 14         } }

to    
(sort) how to sort data  table SetFv  true > false 

table: 0C783508 { [1] =\> table: 0C783508 {          [SetFv] =\> true          [pairing\_id] =\> 1        } [2] =\> table: 0C783508 {          [SetFv] =\> true          [pairing\_id] =\> 2        } [13] =\> table: 0C783508 {           [SetFv] =\> true           [pairing\_id] =\> 13         } [25] =\> table: 0C783508 {           [SetFv] =\> true           [pairing\_id] =\> 14         } [42] =\> table: 0C783508 {          [SetFv] =\> true           [pairing\_id] =\> 14         } [45] =\> table: 0C783508 {           [SetFv] =\> true           [pairing\_id] =\> 14         } [14] =\> table: 0C783508 {           [SetFv] =\> false           [pairing\_id] =\> 14         } [26] =\> table: 0C783508 {           [SetFv] =\> false           [pairing\_id] =\> 14         } [30] =\> table: 0C783508 {          [SetFv] =\> false           [pairing\_id] =\> 14         } }

[lua]

local function sortByBoolean (tbl, key)

  local function compare(first, second)

    return first[key] and not second[key];

  end

   table.sort(tbl, compare)

  return tbl

 end

local sortedTable = sortByBoolean(tableToSort, “SetFv”)

[/lua]

do not work

test 
 

local test = {} test[1] = {SetFv = true,pairing\_id = 1} test[5] = {SetFv = false,pairing\_id = 1} test[6] = {SetFv = false,pairing\_id = 1} test[13] = {SetFv = false,pairing\_id = 1} test[20] = {SetFv = true,pairing\_id = 1} test[21] = {SetFv = false,pairing\_id = 1} test[22] = {SetFv = false,pairing\_id = 1} test[23] = {SetFv = true,pairing\_id = 1} local function sortByBoolean (tbl, key)   local function compare(first, second)     return first[key] and not second[key];   end    table.sort(tbl, compare)   return tbl  end local sortedTable = sortByBoolean(test, "SetFv")

This will only work if your table indexes don’t have any gaps. Any reason why you would want gaps?

if you are only boolean sorting and your dataset is not too big and has gaps in it then do it two pass.

  1. Create a new array

  2. Populate the new array with all the trues

  3. Populate the new array with all the falses

  4. Finally swap your arrays

[lua]

local function sortByBoolean (tbl, key)

  local function compare(first, second)

    return first[key] and not second[key];

  end

   table.sort(tbl, compare)

  return tbl

 end

local sortedTable = sortByBoolean(tableToSort, “SetFv”)

[/lua]

do not work

test 
 

local test = {} test[1] = {SetFv = true,pairing\_id = 1} test[5] = {SetFv = false,pairing\_id = 1} test[6] = {SetFv = false,pairing\_id = 1} test[13] = {SetFv = false,pairing\_id = 1} test[20] = {SetFv = true,pairing\_id = 1} test[21] = {SetFv = false,pairing\_id = 1} test[22] = {SetFv = false,pairing\_id = 1} test[23] = {SetFv = true,pairing\_id = 1} local function sortByBoolean (tbl, key)   local function compare(first, second)     return first[key] and not second[key];   end    table.sort(tbl, compare)   return tbl  end local sortedTable = sortByBoolean(test, "SetFv")

This will only work if your table indexes don’t have any gaps. Any reason why you would want gaps?

if you are only boolean sorting and your dataset is not too big and has gaps in it then do it two pass.

  1. Create a new array

  2. Populate the new array with all the trues

  3. Populate the new array with all the falses

  4. Finally swap your arrays