Page 3 of 3

Re: sorting tables by v

Posted: 21 Aug 2019 08:54
by tatewise
Use a separate sort(a,b) function, so you can debug it just like any other function.

Code: Select all

function sort(a,b)
  if a == nil then
    zz = 0   -- break here to detect local 'a' (a nil value)
  end
  local ans = a.KLIST .. ' ' .. dir .. ' ' .. b.KLIST
  return ans  -- break here to see what is returned
end

table.sort(USRIDX,sort)

Re: sorting tables by v

Posted: 21 Aug 2019 13:30
by Ron Melby
Thanks that solved it.

the error returned is wrong. running around chasing the wrong error.

the upshot is you may not glue up or make the
a < b statement a variable it doesn't do recursive gluing.

it has to be an 'object' statement

so, in my code it will have to be a series of elseifs
if dir = '<' then
table.sort(usridx,( function (a, b) return a < b end)
elseif dir = '>' then
table.sort(usridx,( function (a, b) return a > b end)

and so on ... or perhaps a function table? oh, that would be a new one for me.

Re: sorting tables by v

Posted: 21 Aug 2019 14:14
by tatewise
Yes, a function table will work.

Code: Select all

function SortLess()
	table.sort(usridx,function (a, b) return a < b end)
end

function SortMore()
	table.sort(usridx,function (a, b) return a > b end)
end

local tblFunc = { ["<"]=SortLess; [">"]=SortMore; }
dir = "<"
tblFunc[dir]() -- Invokes SortLess or SortMore depending on character in dir

Re: sorting tables by v

Posted: 21 Aug 2019 14:58
by Ron Melby
I am going to try it when I get home. looks like a simple enough deal

Re: sorting tables by v

Posted: 22 Aug 2019 00:53
by Ron Melby

Code: Select all

Name                            S      Dates       SVCDate  War                    ARM                 Rank              OTH                                                                
COVEY, James Jr. (IV)           M (1715 - 1782) 1756-1763   French and Indian Wars                                                                                                          
COVEY, Joseph Sr.               M (1754 - 1839) Sep 1775    Revolutionary War      Militia             PVT                                                                                  
COVEY, Kenneth William          M (1915 - 1988)             WWII                   US Army             TEC5                                                                                 
COVEY, Marvin Clifford          M (1894 - 1960)             WWI                                                                                                                             
COVEY, Roger L.                 M (1925 - 2004)             WWII                   US Army             TEC5                                                                                 
COVEY, William Luther           M (1825 - 1864) 22 Feb 1864 Civil War              US Army             PVT               Enlisted                                                           
                                                5 May 1864  Civil War              US Army             PVT               Killed in Action                                                   
                                                                                                                                                                                         
COVEY, William Sr.              M (1748 - 1838)             Revolutionary War                                                                                                               
CROSS, Cleo Clair               M (1921 - 1982)             WWII                   US Army             CPL                                                                                  

ok, that is looking pretty good, it is working as I had imagined. I have to work on my centering routine, and make a table lookahead field, so that where I have someone like William Luther Covey who has multiple entries, I will put a line before as well as a line after.