* sorting trouble

For plugin authors to discuss plugin programming
Post Reply
User avatar
Ron Melby
Megastar
Posts: 944
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

sorting trouble

Post by Ron Melby »

I am having a load of trouble with what I think should be a simple sort.
This is how I want the data sorted:


GEN: -86 PTG: SIDE: uix:
GEN: -6 PTG: SIDE: uix:

GEN: -14 PTG: 2 SIDE: 2 uix: 28
GEN: -13 PTG: 2 SIDE: 2 uix:

GEN: -5 PTG: 2 SIDE: 3 uix:
GEN: -1 PTG: 2 SIDE: 3 uix:

GEN: -1 PTG: 3 SIDE: 2 uix: 3621
GEN: 2 PTG: 3 SIDE: 2 uix: 1

GEN: -14 PTG: 3 SIDE: 3 uix: 152
GEN: - 4 PTG: 3 SIDE: 3 uix: 7

the range of values for each column
GEN -86, -14 ... 4 (at present)
PTG '', 2, 3, 4
SIDE '', 2, 3, 4
uix '', 0 ... n

the columns and their meanings are as follows:
GEN - generation from root, which is gen 0. GEN always has a number. -86 means I cannot find the generation, YET! daughter = 1, granddaughter = 2, father = -1, grandfather -2, and so on.

PTG and SIDE work together:
PTG - 2 = father, 3 = mother
SIDE - 2 = grandfather 3 = grandmother
PTG: '' SIDE: '' = cannot find this information, YET!
PTG: 2 SIDE: 2 = father's father's side (male's male line)
PTG: 2 SIDE: 3 = father's mother's side (male's female line)
PTG: 3 SIDE: 2 = mother's father's side (female's male line)
PTG: 3 SIDE: 3 = mother's mother's side (female's female line)
PTG: 4 SIDE: 4 = root family

uix - '' = not direct line (i.e wives and husbands of relations, no relations), some number n = direct relation (brother/sister/father/mother/grandfather/grandmother, etc)

so I would like the PTG/SIDE grouped together ascending, with GEN ascending and uix ascending (there is a problem there with uix to be worked out later)
this is my current iteration of the sort, and it does not result in the order above (this is not the first iteration I have tried):

table.sort(_tbl,
function(a, b)
if tostring(a.PTG) == tostring(b.PTG) then
if tostring(a.SIDE) == tostring(b.SIDE) then
if tostring(a.GEN) == tostring(b.GEN) then
return tostring(a.uix) < tostring(b.uix)
end
return tostring(a.GEN) < tostring(b.GEN)
end
return tostring(a.SIDE) < tostring(b.SIDE)
end
return tostring(a.PTG) < tostring(b.PTG)
end
)
can someone help me get there? thanks.
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 29154
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: sorting trouble

Post by tatewise »

Why are you using the tostring(...) function?
That will sort alphanumerically as text, i.e. "1", "11", "12", "13", etc, will sort before "2", "21", "22", "23", etc.
Since the values are all numbers just use a.GEN < b.GEN etc.

If they are not numbers but text versions of numbers, then use the tonumber(...) function instead.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 944
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: sorting trouble

Post by Ron Melby »

if one actually reads the text of my problem statement in which I tried to give maximum understanding of my problem, any field other than GEN can contain either a NUMBER or a void STRING.( i.e. PTG = '' or PTG = 2). my removing the tostring from GEN does not get the sort I want.
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 29154
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: sorting trouble

Post by tatewise »

OK, but using tostring(...) does not resolve that and results in non-numeric sorting as I explained.
Apart those empty strings are all other values actually numeric, i.e. 123 and not text "123"?
Trying to sort numbers and empty text strings is unlikely to be straightforward as they are different data types.
I don't remember how a.PTG < b.PTG handles numbers versus an empty string. You'll have to experiment.
OR you'll have to test for the empty string values as a special case.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
avatar
jelv
Megastar
Posts: 787
Joined: 03 Feb 2020 22:57
Family Historian: V7
Location: Mere, Wiltshire

Re: sorting trouble

Post by jelv »

To sort as strings you'd need to determine the maximum length of each number and pad the values out with leading zeroes.
John Elvin
User avatar
tatewise
Megastar
Posts: 29154
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: sorting trouble

Post by tatewise »

It would be easier to replace '' with 0 and sort by numbers.

Since, tonumber(...) returns nil for non-numbers it is quite easy to perform that substitution.
e.g.
return ( tonumber(a.PTG) or 0 ) < ( tonumber(b.PTG) or 0 )

Thus if PTG is a number then that value is tested, else if it is '' then tonumber() returns nil so or substitutes 0.
i.e.
when a.PTG = 8 then ( tonumber(a.PTG) or 0 ) = 8
when a.PTG = '' then ( tonumber(a.PTG) or 0 ) = 0
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 944
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: sorting trouble

Post by Ron Melby »

FAM: Aker, Anders and Raaen, Randi Aslesdatter
FAM : [ 2796] GEN: -86 PTG: -1 SIDE: -1 COL: uix: -1
HUSB : [ 6753] GEN: -86 PTG: -1 SIDE: -1 COL: uix: RLT:
WIFE : [ 6754] GEN: -86 PTG: -1 SIDE: -1 COL: uix: RLT:
CHIL : [ 6752] GEN: -4 PTG: -1 SIDE: -1 COL: uix: RLT:

FAM: Covey, Nolan Wesson Jr. and Wendling, Elke Anna Maria
FAM : [ 1878] GEN: -3 PTG: -1 SIDE: -1 COL: uix: -1
HUSB : [ 4536] GEN: -3 PTG: -1 SIDE: -1 COL: 11.03 uix: RLT: 10th cousin (03x) removed
WIFE : [ 4537] GEN: -3 PTG: -1 SIDE: -1 COL: 11.03 uix: RLT: 10th cousin (03x) removed's wife
CHIL : [ 4538] GEN: -2 PTG: -1 SIDE: -1 COL: 12.02 uix: RLT: 11th cousin (02x) removed
CHIL : [ 4539] GEN: -2 PTG: -1 SIDE: -1 COL: 12.02 uix: RLT: 11th cousin (02x) removed
CHIL : [ 4540] GEN: -2 PTG: -1 SIDE: -1 COL: 12.02 uix: RLT: 11th cousin (02x) removed
CHIL : [ 4541] GEN: -2 PTG: -1 SIDE: -1 COL: 12.02 uix: RLT: 11th cousin (02x) removed
CHIL : [ 4542] GEN: -2 PTG: -1 SIDE: -1 COL: 12.02 uix: RLT: 11th cousin (02x) removed

they are grouping by ptg/side it appears but the gen does not sort from least -14 to 4 (most)
there is a problem with 0 in most columns it means something in and of itself.

FAM: Faafeng, Ole Nilsen and Henriksen, Oluffa Kathinka
FAM : [ 2775] GEN: -3 PTG: -1 SIDE: -1 COL: uix: -1
HUSB : [ 6698] GEN: -3 PTG: 2 SIDE: 2 COL: uix: RLT:
WIFE : [ 6700] GEN: -3 PTG: -1 SIDE: -1 COL: uix: RLT:
CHIL : [ 6701] GEN: -2 PTG: -1 SIDE: -1 COL: uix: RLT:

FAM: Covey, Nolan White Sr. and Campbell, Elinor Louise
FAM : [ 1880] GEN: -4 PTG: -1 SIDE: -1 COL: uix: -1
HUSB : [ 4543] GEN: -4 PTG: -1 SIDE: -1 COL: 10.04 uix: RLT: 9th cousin (04x) removed
WIFE : [ 4546] GEN: -4 PTG: -1 SIDE: -1 COL: 10.04 uix: RLT: 9th cousin (04x) removed's wife

FAM: Hastings, Roger Edward and Melby, Carol Jean
FAM : [ 1037] GEN: -1 PTG: 2 SIDE: 2 COL: 2.01 uix: 254
HUSB : [ 2609] GEN: -1 PTG: 2 SIDE: 2 COL: 2.01 uix: RLT: 1st cousin (01x) removed's husband
WIFE : [ 2608] GEN: -1 PTG: 2 SIDE: 2 COL: 2.01 uix: 254 RLT: 1st cousin (01x) removed
CHIL : [ 2925] GEN: 0 PTG: 2 SIDE: 2 COL: 3 uix: 255 RLT: 2nd cousin
CHIL : [ 2924] GEN: 0 PTG: 2 SIDE: 2 COL: 3 uix: 256 RLT: 2nd cousin

FAM: Hopstock, Jacob and Grøndahl, Else
FAM : [ 2771] GEN: -2 PTG: 2 SIDE: 2 COL: 3.01 uix: -1
HUSB : [ 6690] GEN: -2 PTG: 2 SIDE: 2 COL: uix: RLT:
WIFE : [ 6691] GEN: -2 PTG: 2 SIDE: 2 COL: uix: RLT:
CHIL : [ 2649] GEN: -1 PTG: 2 SIDE: 2 COL: 3.01 uix: RLT: 2nd cousin (01x) removed's husband


this is the sort now:
table.sort(_tbl,
function(a, b)
if a.PTG == '' then a.PTG = -1 a.SIDE = -1 end
if b.PTG == '' then b.PTG = -1 b.SIDE = -1 end
if a.uix == '' then a.uix = -1 end
if b.uix == '' then b.uix = -1 end

if a.PTG == b.PTG then
if a.SIDE == b.SIDE then
if a.GEN == b.GEN then
return a.uix < b.uix
end
return a.GEN < b.GEN
end
return a.SIDE < b.SIDE
end
return a.PTG < b.PTG
end
)

return _tbl
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 29154
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: sorting trouble

Post by tatewise »

As far as I can tell the sort is behaving correctly according to your coding.
i.e.
It primarily sorts on PTG and SIDE. But if those both have the same values then it sorts on GEN.

Please give an example of _tbl values that have not sorted corrected showing the actual sorted order produced by the plugin script and what you want the sorted order to be.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 944
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: sorting trouble

Post by Ron Melby »

for PTG, SIDE, and COL (not at issue yet) rather than initialize as '' (void string), I have initialized as ' ' (single blank string) and changed their 'number' (2, 3, 4) to string ('2', '3', '4').

this has caused several of my functions to not produce results as expected so I am working thru that.
the one giving me fits is the assembly function (because it is rather random) for k, v in ipairs(FAM) do
use INDI records to fill out missing data in the FAM record, and bootstrapping up the FAM to its rightful place.

i.e, this:
FAM: Covey, Joseph and Neff, Jerusha
FAM : [ 1854] GEN: -9 PTG: SIDE: COL: uix:
HUSB : [ 4428] GEN: -9 PTG: 3 SIDE: 3 COL: 1 uix: 2415 RLT: great (x07) grand uncle
WIFE : [ 4478] GEN: -9 PTG: SIDE: COL: 1 uix: RLT: great (x07) grand uncle's wife
CHIL : [ 4479] GEN: -8 PTG: 3 SIDE: 3 COL: 2.08 uix: 2416 RLT: 1st cousin (08x) removed
CHIL : [ 4480] GEN: -8 PTG: 3 SIDE: 3 COL: 2.08 uix: 2417 RLT: 1st cousin (08x) removed
CHIL : [ 4481] GEN: -8 PTG: 3 SIDE: 3 COL: 2.08 uix: 2418 RLT: 1st cousin (08x) removed

should become this:
FAM: Covey, Joseph and Neff, Jerusha
FAM : [ 1854] GEN: -9 PTG: 3 SIDE: 3 COL: 1 uix: 2415
HUSB : [ 4428] GEN: -9 PTG: 3 SIDE: 3 COL: 1 uix: 2415 RLT: great (x07) grand uncle
WIFE : [ 4478] GEN: -9 PTG: 3 SIDE: 3 COL: 1 uix: RLT: great (x07) grand uncle's wife
CHIL : [ 4479] GEN: -8 PTG: 3 SIDE: 3 COL: 2.08 uix: 2416 RLT: 1st cousin (08x) removed
CHIL : [ 4480] GEN: -8 PTG: 3 SIDE: 3 COL: 2.08 uix: 2417 RLT: 1st cousin (08x) removed
CHIL : [ 4481] GEN: -8 PTG: 3 SIDE: 3 COL: 2.08 uix: 2418 RLT: 1st cousin (08x) removed


It """"APPEARS"""" as though it is sorting correctly now.

thanks. more to follow I am sure.
FH V.6.2.7 Win 10 64 bit
User avatar
Ron Melby
Megastar
Posts: 944
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: sorting trouble

Post by Ron Melby »

the sort works in that ' ' (string 1 blank) is less than string '2' or '3' or '4'
when I initialized the fields to '' (void string) this code used to work using '' instead of ' '.

if type(F.PTG) > ' ' and type(H.PTG) == ' ' then
mat_rcdseg(H, {PTG = F.PTG, SIDE = F.SIDE,})
elseif type(H.PTG) > ' ' and type(F.PTG) == ' ' then
mat_rcdseg(F, {PTG = H.PTG, SIDE = H.SIDE, COL = H.COL, uix = H.uix,})
end

F is the FAM record and H is the HUSB[iID] INDI record

F =
fID => 1
FAM => "Newton, Dale Arden and Aune, Mavis Elaine"
GEN => 0
PTG => " "
SIDE => " "
COL => " "
uix => " "
HUSB => (table .2)
NAME => "Newton, Dale Arden "
iID => 95
WIFE => (table .2)
NAME => "Aune, Mavis Elaine "
iID => 7162
CHIL => (table #2)
[1] => (table .2)
NAME => "Newton, Jeffrey Dale "
iID => 100
[2] => (table .2)
NAME => "Newton, Kristi Lynn "
iID => 103

H =
iID => 95
GEN => 0
PTG => "3"
SIDE => "3"
COL => 3
uix => 878
NAME => "Newton, Dale Arden "
RLT => " 2nd cousin"

I believe the code above should execute the elseif and fill the FAM record fields using the H INDI fields, but the elseif does not execute.
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 29154
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: sorting trouble

Post by tatewise »

What do you think the type(...) functions return?

How do you think they compare with '' or ' ' ?
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 944
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: sorting trouble

Post by Ron Melby »

in each case I would expect the type to return string for '', ' ', and '2'
i would expect the value of ' ' to be either 0020 or 20 and the value of '2' to be 0032 or 32
i would expect '2' to be > ' '
FH V.6.2.7 Win 10 64 bit
avatar
jelv
Megastar
Posts: 787
Joined: 03 Feb 2020 22:57
Family Historian: V7
Location: Mere, Wiltshire

Re: sorting trouble

Post by jelv »

Suggest you look at the manual!
type (v)
Returns the type of its only argument, coded as a string. The possible results of this function are "nil" (a string, not the value nil), "number", "string", "boolean", "table", "function", "thread", and "userdata".
type(H.PTG) == ' ' will never be true.
John Elvin
User avatar
Ron Melby
Megastar
Posts: 944
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: sorting trouble

Post by Ron Melby »

void = ''
blank = ' '
nbr = '2'

v = type(void)
b = type(blank)
n = type(nbr)
return

all return string, as I stated.

D OH!!! I didnt clear type statement, it used to be type = string and type = number but now they are strings its direct compare,.... I would have been blind to the type functions there for years.... thanks. I feel so cheap.
FH V.6.2.7 Win 10 64 bit
Post Reply