# HP Forums

Full Version: How to sort two lists? Sort distance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.
sort(l,(x,y)->string(x)<string(y))
(04-29-2016 07:33 PM)parisse Wrote: [ -> ]sort(l,(x,y)->string(x)<string(y))

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.
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)<string(y));    END;```
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
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 }} ?
(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)<string(y));    END;```
You should probably enter a CAS program if you want to use CAS functions like sort with functional arguments.
(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 }} ?

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 }})`
(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.

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.
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-
I found an old thread relevant to sorting (I missed it before): http://www.hpmuseum.org/forum/thread-429...light=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;```
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
(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.

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}}
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 .
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,
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;```
(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}
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]
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: