HP Forums
How to sort two lists? Sort distance - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: How to sort two lists? Sort distance (/thread-6179.html)

Pages: 1 2


How to sort two lists? Sort distance - StephenG1CMZ - 04-29-2016 07:06 PM

Is there a way to sort multiple lists

For example, I have a list of elements
{
{1,H,hydrogen},
{2,He,helium}
}

Which I'd like to present to the user in Alphabetic order of atomic number, symbol, or element name.

I know how to sort manually, but is there a built-in that would help?

SORT looks like it will only sort one list, rather than sorting a list by a key.

I am sure I read of a way, but cannot find it.


RE: How to sort two lists? - parisse - 04-29-2016 07:33 PM

sort(l,(x,y)->string(x[3])<string(y[3]))


RE: How to sort two lists? - primer - 04-29-2016 09:22 PM

(04-29-2016 07:33 PM)parisse Wrote:  sort(l,(x,y)->string(x[3])<string(y[3]))

wow, such a wonderfull syntax !

It remind me a mix between an old-C-style callback qsort() way-of-working with a kind of modern-lambda-function syntax.

just incredible to see that on a calc.
thank you.


RE: How to sort two lists? - StephenG1CMZ - 04-30-2016 10:04 AM

Sorry...I am a novice with the the CAS functions.
This program won't compile - where am I going wrong?
Code:


 LOCAL l:={
  {1,"H","Hydro"},
  {2,"He","Heli"}}; 
 //LOCAL x,y;
 EXPORT ASORT()
 BEGIN
  //SORT()
  sort(l); //OK
  sort(l,(x,y)->string(x[3])<string(y[3]));
 
 END;



RE: How to sort two lists? - cyrille de brébisson - 05-02-2016 04:56 AM

Hello,

in Home, you can use the SORT(List, item in list to sort by) syntax.
Sorry, but the help for SORT does not seem to have been updated...

Anyhow, SORT now takes 2 parameters, the list to sort and one or more sequence of sort indexes...

assuming that L1 is a list of lists.
so, SORT(L1, a) sorts by element a of the sub lists
SORT(L1, {a, b, c}) sorts by element a, then b, then c of the sub list (b is used if elements a are equal...)
you can even do
SORT(L1, {{a, b, c}, d}) in this case {a, b, c} means sort L1 by the element (a, b, c) of L1, is recall cth element of the bth list of the ath list of L1!
ie, the {a, b, c}, follows the same rules as L1(parameters)... you can of course use this, to sort by nth element in matrices in lists and the like...

Cyrille


RE: How to sort two lists? - Tyann - 05-02-2016 05:32 AM

Bonjour

Je ne comprends pas comment cela fonctionne .
Si j'ai {{3,2,1},{15,20,18}} et que je veux {{1,2,3},{18,20,15}}
Comment je fait ?
SORT({{3,2,1},{15,20,18}},1) -> {{3,2,1},{15,20,18}} ?
Merci d'avance

Hello

I do not understand how it works .
If I {{ 3,2,1 }, { 15,20,18 }} and I want to { {1,2,3 }, { 18,20,15 }}
How I do ?
SORT ( {{ 3,2,1 }, { 15,20,18 }} , 1) - > { { 3,2,1 }, { 15,20,18 }} ?
thank you in advance


RE: How to sort two lists? - parisse - 05-02-2016 05:44 AM

(04-30-2016 10:04 AM)StephenG1CMZ Wrote:  Sorry...I am a novice with the the CAS functions.
This program won't compile - where am I going wrong?
Code:


 LOCAL l:={
  {1,"H","Hydro"},
  {2,"He","Heli"}}; 
 //LOCAL x,y;
 EXPORT ASORT()
 BEGIN
  //SORT()
  sort(l); //OK
  sort(l,(x,y)->string(x[3])<string(y[3]));
 
 END;
You should probably enter a CAS program if you want to use CAS functions like sort with functional arguments.


RE: How to sort two lists? - Didier Lachieze - 05-02-2016 06:57 AM

(05-02-2016 05:32 AM)Tyann Wrote:  I do not understand how it works .
If I {{ 3,2,1 }, { 15,20,18 }} and I want to { {1,2,3 }, { 18,20,15 }}
How I do ?
SORT ( {{ 3,2,1 }, { 15,20,18 }} , 1) - > { { 3,2,1 }, { 15,20,18 }} ?
thank you in advance

I don't think that SORT can work inside sub-list items, here is a way to do what you want:
Code:
L1:={{ 3,2,1 }, { 15,20,18 }}
MAKELIST(SORT(L1(I)),I,1,SIZE(L1))

EDIT: a shorter way:
Code:
EXECON("SORT(&1)",{{ 3,2,1 }, { 15,20,18 }})



RE: How to sort two lists? - Tyann - 05-02-2016 10:35 AM

(05-02-2016 06:57 AM)Didier Lachieze Wrote:  I don't think that SORT can work inside sub-list items, here is a way to do what you want:
Code:
L1:={{ 3,2,1 }, { 15,20,18 }}
MAKELIST(SORT(L1(I)),I,1,SIZE(L1))

EDIT: a shorter way:
Code:
EXECON("SORT(&1)",{{ 3,2,1 }, { 15,20,18 }})

Merci pour vôtre réponse, mais vôtre solution ne convient pas.
Vous triez les 2 listes par odre ascendant. -> {{1,2,3},{15,18,20}}

Moi je veux trier la liste 1 et que la liste 2 suive la liste 1 ->{{1,2,3},{18,20,15}}
J'espére que la nouvelle syntaxe autorise cela.

Thank you for your answer, but yours does not fit .
You sort the two lists in ascending order. -> { {1,2,3 }, { 15,18,20 }}

I want my sort List 1 and List 2 follows the list 1 - > { {1,2,3 }, { 18,20,15 }}
I hope that the new syntax allows this.


RE: How to sort two lists? - DrD - 05-02-2016 11:11 AM

It sounds like you might want one to to one correspondence between the two lists. Sorting one, changes the relative position of the other. What if there were more than two lists? How would subsequent sorting work, or in the case where there are more than two lists, are you suggesting to ONLY sort on one specified list, with all others adjusted to the relative position of the sorted list?

That gets kind of convoluted! It's possible to do that programmatically, but I think probably too rigorous for a single command variant.

-Dale-


RE: How to sort two lists? - StephenG1CMZ - 05-02-2016 12:11 PM

I found an old thread relevant to sorting (I missed it before): http://www.hpmuseum.org/forum/thread-4294.html?highlight=SORT
"Best way to sort one list based on another".

But the latest suggestion using SORT's undocumented second parameter isn't compiling:
Code:


 EXPORT BSORT()
 BEGIN
  L1:={
   {3,"H", "Hydrogen"},
   {2,"He","Helium"}};
  SORT(L1);//SYNTAX OK-UNSORTED
  L2:=SORT(L1,{2}); //SYNTAX?
 
 END;



RE: How to sort two lists? - cyrille de brébisson - 05-02-2016 03:34 PM

Hello,

SORT with an extra argument assumes that connex data is in a single sub list of the list to sort.

To do a sort as you want, with 2 or more lists which represent a 1 to 1 correspondence, you could transpose the list first, sort on that and transpose back.

This would work if you use the CAS to do the transpose as the CAS sees lists as matrices and vice-versa.

Cyrille


RE: How to sort two lists? - Didier Lachieze - 05-02-2016 04:00 PM

(05-02-2016 10:35 AM)Tyann Wrote:  Merci pour vôtre réponse, mais vôtre solution ne convient pas.
Vous triez les 2 listes par odre ascendant. -> {{1,2,3},{15,18,20}}

Moi je veux trier la liste 1 et que la liste 2 suive la liste 1 ->{{1,2,3},{18,20,15}}
J'espére que la nouvelle syntaxe autorise cela.

Thank you for your answer, but yours does not fit .
You sort the two lists in ascending order. -> { {1,2,3 }, { 15,18,20 }}

I want my sort List 1 and List 2 follows the list 1 - > { {1,2,3 }, { 18,20,15 }}
I hope that the new syntax allows this.

Sorry for the misunderstanding. Here is a program doing what you want:

Code:
EXPORT Sortn(list,n)
BEGIN
  LOCAL li,ma;
  ma:=list2mat(list) ;
  li:=SORT(MAKELIST(mat2list(col(ma,I)),I,1,colDim(ma)),n);
  RETURN MAKELIST(mat2list(col(list2mat(li),I)),I,1,rowDim(ma));
END;

For example:

Sortn({{3,2,1},{15,20,18}},1) -> {{1,2,3},{18,20,15}}

Sortn({{3,2,1},{15,20,18},{32,35,27}},1) -> {{1,2,3},{18,20,15},{27,35,32}}
Sortn({{3,2,1},{15,20,18},{32,35,27}},2) -> {{3,1,2},{15,18,20},{32,27,35}}
Sortn({{3,2,1},{15,20,18},{32,35,27}},3) -> {{1,3,2},{18,15,20},{27,32,35}}


RE: How to sort two lists? - Tyann - 05-02-2016 05:10 PM

Quote:It sounds like you might want one to to one correspondence between the two lists. Sorting one, changes the relative position of the other. What if there were more than two lists? How would subsequent sorting work, or in the case where there are more than two lists, are you suggesting to ONLY sort on one specified list, with all others adjusted to the relative position of the sorted list?

That gets kind of convoluted! It's possible to do that programmatically, but I think probably too rigorous for a single command variant.

Oui, si il y a plusieurs listes, les autres suivent le mouvement de la première la commande SORT
du Ti BASIC le fait très bien.
Yes, if there are several lists , the others follow the movement of the first , the SORT command
Ti BASIC works fine .

Quote:Sorry for the misunderstanding. Here is a program doing what you want:
Merci beaucoup, j'avais moi aussi une solution programmée, mais la vôtre est beaucoup plus
courte.
Thank you very much , I too had a programmed solution , but yours is much
short.
Cependant la mienne si on lui transmet une seule liste, en renvoie 2 la liste triée et
une liste d'index selon l'ordre de tri.
However mine if he will receive one list , returns 2 in the sorted list and
an index list in the sort order .


RE: How to sort two lists? - ailoher - 12-03-2019 10:24 AM

Hello,

Sorry for refloating this old post, but I am having a similar concern.

From what I had read in here, it would seem that the documentation of 'sort' command is outdated. From what I know about it, it sorts the elements in a list in ascending order. What I would need is to sort the elements from a list with respect to their distance to an element outside the list. Is there any way to do so?

Many thanks in advance, and may you have a nice day.

Best regards,


RE: How to sort two lists? - StephenG1CMZ - 12-03-2019 04:43 PM

I am not aware of a built-in for that.
I think this outlines what you want...
Although you will probably prefer to make the MYDIST function a parameter
(update: see later post)
Code:

 EXPORT MYDIST(LST,ITEM)
 //EXAMPLE DISTANCE FUNCTION:
 //USE YOUR OWN
 BEGIN
  RETURN ABS(LST-ITEM);
 END;

 EXPORT DIST(LST,ITEM)
 BEGIN
  RETURN MYDIST(LST,ITEM);
 END;

 EXPORT SORTDIST(LST,ITEM)
 BEGIN
  LOCAL DISTLST:=DIST(LST,ITEM);
  LOCAL SORTED:=Sortn({LST,DISTLST},2);
  RETURN SORTED(1);
 END;
 
EXPORT LISTER()
//EXAMPLE
BEGIN
  LOCAL TESTLST:={10,20,5};
  LOCAL ITEM:=1;
  RETURN SORTDIST(TESTLST,ITEM);
END;



RE: How to sort two lists? - Didier Lachieze - 12-03-2019 05:02 PM

(12-03-2019 10:24 AM)ailoher Wrote:  What I would need is to sort the elements from a list with respect to their distance to an element outside the list. Is there any way to do so?

Here is a CAS program that should do what you want, if I've correctly understood it:

Code:
#cas
sortd(li,ref):=
BEGIN
 return sort(li,(x,y)->abs(x-ref)<abs(y-ref));
END;
#end

sortd({2,3,6,8,5,9},5) returns {5,6,3,2,8,9}


RE: How to sort two lists? - Albert Chan - 12-03-2019 06:00 PM

Schwartzian transform (DSU sort)

XCas> lst := [2,3,6,8,5,9]
XCas> tran(sort(map(lst, x->[abs(x-5), x])))(2)     → [5,6,3,2,8,9]


RE: How to sort two lists? - StephenG1CMZ - 12-03-2019 07:30 PM

Here is a version that works with your choice of sort (distance) function as a parameter:

Code:

 EXPORT MYSORTFUN(LST,ITEM)
 //EXAMPLE DISTANCE FUNCTION: USE YOUR OWN
 BEGIN
  RETURN ABS(LST-ITEM);
 END;

 EXPORT SortByFun(LST,SortFun)
 //Sort using SortFun as your sort function
 //EG SortByFun(LST,MYDIST)
 BEGIN
  LOCAL SORTORDER:=SortFun;
  LOCAL SORTED:=Sortn({LST,SORTORDER},2);
  RETURN SORTED(1); //TO SEE THE SORT ORDER:2
 END;

 EXPORT SORTER()
 //EXAMPLE
 BEGIN
  LOCAL TESTLST:={10,20,5};
  LOCAL ITEM:=10;
  RETURN SortByFun(TESTLST,MYSORTFUN(TESTLST,ITEM));  
 END;

SortByFun has now been added to my collection of sort routines:
https://www.hpmuseum.org/forum/thread-11365-post-124491.html#pid124491

Ailoher, did you have any specific sort distance function in mind? Or is ABS(LST-Item) OK?


RE: How to sort two lists? - ailoher - 12-05-2019 06:55 PM

Thank you for all the replies! They were exactly what I needed.

Kind regards,
Aitor