Re: Phix:Sort Columns Not Working Like Expected
- Posted by petelomax Jun 24, 2020
- 1849 views
As tom said, that is what I would expect. I'll start with a quick diversion of a new feature and an explanation/demonstration of what sort_columns actually does.
On the basis of this I have just added pp_IntCh of -1 such that
pp(table) -- {{5,4,3,2,1}, {65'A',66'B',67'C',68'D',69'E'}} pp(table,{pp_IntCh,-1}) -- {{5,4,3,2,1}, {'A','B','C','D','E'}}
Without that new feature:
sequence ic = columnize({{5,4,3,2,1},{'A','B','C','D','E'}}) pp(ic) -- {{5,65'A'}, {4,66'B'}, {3,67'C'}, {2,68'D'}, {1,69'E'}} pp(sort_columns(ic,{1})) -- {{1,69'E'}, {2,68'D'}, {3,67'C'}, {4,66'B'}, {5,65'A'}} pp(sort_columns(ic,{2})) -- {{5,65'A'}, {4,66'B'}, {3,67'C'}, {2,68'D'}, {1,69'E'}}
With that new feature:
sequence ic = columnize({{5,4,3,2,1},{'A','B','C','D','E'}}) ppOpt({pp_IntCh,-1}) pp(ic) -- {{5,'A'}, {4,'B'}, {3,'C'}, {2,'D'}, {1,'E'}} pp(sort_columns(ic,{1})) -- {{1,'E'}, {2,'D'}, {3,'C'}, {4,'B'}, {5,'A'}} pp(sort_columns(ic,{2})) -- {{5,'A'}, {4,'B'}, {3,'C'}, {2,'D'}, {1,'E'}}
That is how I would naturally get the order you want, but it ain't in the format you want.
Getting back to your immediate needs, the thing is that sort can only sort one thing, but then again a tag sort can be applied to as many things as you like, so:
sequence table = {{5,4,3,2,1},{'A','B','C','D','E'}} sequence tags = custom_sort(table[1],tagset(length(table[1]))) sequence res = table -- (just for the shape, every element gets overwritten) for i=1 to length(tags) do for j=1 to length(table) do res[j][i] = table[j][tags[i]] end for end for pp(res) -- {{1,2,3,4,5}, {69'E',68'D',67'C',66'B',65'A'}} ppOpt({pp_IntCh,-1}) pp(res) -- {{1,2,3,4,5}, {'E','D','C','B','A'}}
Does that help?
I can see there might be a basis somewhere among all that for a new builtin to live alongside sort_columns() and custom_sort(), but what to call it?... (column_sort()?!)
PS: I should strongly recommend going the columnize() route, defining 5 and 'A' next to each other to start with, as per the pp(ic) outputs. Inserting/deleting the 3rd element of 54321 and ABCDE looks simple enough, but once they're 50+ elements long it gets to be a RPITA keeping two or more such sequences perfectly in step. Note that when I say "columnize() route", you should understand there is a reversibility about that:
sequence ic = columnize({{5,4,3,2,1},{'A','B','C','D','E'}}) ppOpt({pp_IntCh,-1}) pp(ic) -- {{5,'A'}, {4,'B'}, {3,'C'}, {2,'D'}, {1,'E'}} pp(columnize(ic)) -- {{5,4,3,2,1}, {'A','B','C','D','E'}}