How to sort two lists? Sort distance
04-29-2016, 07:06 PM (This post was last modified: 12-06-2019 09:38 PM by StephenG1CMZ.)
Post: #1
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
How to sort two lists? Sort distance
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.

Stephen Lewkowicz (G1CMZ)
04-29-2016, 07:33 PM
Post: #2
 parisse Senior Member Posts: 1,243 Joined: Dec 2013
RE: How to sort two lists?
sort(l,(x,y)->string(x[3])<string(y[3]))
04-29-2016, 09:22 PM
Post: #3
 primer Member Posts: 135 Joined: Sep 2015
RE: How to sort two lists?
(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.

primer
04-30-2016, 10:04 AM (This post was last modified: 04-30-2016 06:56 PM by StephenG1CMZ.)
Post: #4
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
RE: How to sort two lists?
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;

Stephen Lewkowicz (G1CMZ)
05-02-2016, 04:56 AM
Post: #5
 cyrille de brébisson Senior Member Posts: 1,047 Joined: Dec 2013
RE: How to sort two lists?
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

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
05-02-2016, 05:32 AM
Post: #6
 Tyann Member Posts: 257 Joined: Oct 2014
RE: How to sort two lists?
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 }} ?

Sorry for my english
05-02-2016, 05:44 AM
Post: #7
 parisse Senior Member Posts: 1,243 Joined: Dec 2013
RE: How to sort two lists?
(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.
05-02-2016, 06:57 AM (This post was last modified: 05-02-2016 07:10 AM by Didier Lachieze.)
Post: #8
 Didier Lachieze Senior Member Posts: 1,539 Joined: Dec 2013
RE: How to sort two lists?
(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, 10:35 AM
Post: #9
 Tyann Member Posts: 257 Joined: Oct 2014
RE: How to sort two lists?
(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.

Sorry for my english
05-02-2016, 11:11 AM
Post: #10
 DrD Senior Member Posts: 1,133 Joined: Feb 2014
RE: How to sort two lists?
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-
05-02-2016, 12:11 PM (This post was last modified: 05-02-2016 12:17 PM by StephenG1CMZ.)
Post: #11
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
RE: How to sort two lists?
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;

Stephen Lewkowicz (G1CMZ)
05-02-2016, 03:34 PM
Post: #12
 cyrille de brébisson Senior Member Posts: 1,047 Joined: Dec 2013
RE: How to sort two lists?
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

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
05-02-2016, 04:00 PM
Post: #13
 Didier Lachieze Senior Member Posts: 1,539 Joined: Dec 2013
RE: How to sort two lists?
(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}}
05-02-2016, 05:10 PM (This post was last modified: 05-02-2016 05:15 PM by Tyann.)
Post: #14
 Tyann Member Posts: 257 Joined: Oct 2014
RE: How to sort two lists?
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 .

Sorry for my english
12-03-2019, 10:24 AM
Post: #15
 ailoher Junior Member Posts: 4 Joined: Oct 2019
RE: How to sort two lists?
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,
12-03-2019, 04:43 PM (This post was last modified: 12-03-2019 08:01 PM by StephenG1CMZ.)
Post: #16
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
RE: How to sort two lists?
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;

Stephen Lewkowicz (G1CMZ)
12-03-2019, 05:02 PM
Post: #17
 Didier Lachieze Senior Member Posts: 1,539 Joined: Dec 2013
RE: How to sort two lists?
(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}
12-03-2019, 06:00 PM
Post: #18
 Albert Chan Senior Member Posts: 2,101 Joined: Jul 2018
RE: How to sort two lists?
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]
12-03-2019, 07:30 PM (This post was last modified: 12-06-2019 09:35 PM by StephenG1CMZ.)
Post: #19
 StephenG1CMZ Senior Member Posts: 945 Joined: May 2015
RE: How to sort two lists?
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:

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

Stephen Lewkowicz (G1CMZ)
12-05-2019, 06:55 PM
Post: #20
 ailoher Junior Member Posts: 4 Joined: Oct 2019
RE: How to sort two lists?
Thank you for all the replies! They were exactly what I needed.

Kind regards,
Aitor
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)