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
|
|||
|
|||
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) https://my.numworks.com/python/steveg1cmz |
|||
04-29-2016, 07:33 PM
Post: #2
|
|||
|
|||
RE: How to sort two lists?
sort(l,(x,y)->string(x[3])<string(y[3]))
|
|||
04-29-2016, 09:22 PM
Post: #3
|
|||
|
|||
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
|
|||
|
|||
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:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
05-02-2016, 04:56 AM
Post: #5
|
|||
|
|||
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
|
|||
|
|||
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 }} ? thank you in advance Sorry for my english |
|||
05-02-2016, 05:44 AM
Post: #7
|
|||
|
|||
RE: How to sort two lists?
(04-30-2016 10:04 AM)StephenG1CMZ Wrote: Sorry...I am a novice with the the CAS functions.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
|
|||
|
|||
RE: How to sort two lists?
(05-02-2016 05:32 AM)Tyann Wrote: I do not understand how it works . 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 }} EDIT: a shorter way: Code: EXECON("SORT(&1)",{{ 3,2,1 }, { 15,20,18 }}) |
|||
05-02-2016, 10:35 AM
Post: #9
|
|||
|
|||
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: 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 my english |
|||
05-02-2016, 11:11 AM
Post: #10
|
|||
|
|||
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
|
|||
|
|||
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:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
05-02-2016, 03:34 PM
Post: #12
|
|||
|
|||
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
|
|||
|
|||
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. Sorry for the misunderstanding. Here is a program doing what you want: Code: EXPORT Sortn(list,n) 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
|
|||
|
|||
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? 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
|
|||
|
|||
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
|
|||
|
|||
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:
Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
12-03-2019, 05:02 PM
Post: #17
|
|||
|
|||
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({2,3,6,8,5,9},5) returns {5,6,3,2,8,9} |
|||
12-03-2019, 06:00 PM
Post: #18
|
|||
|
|||
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
|
|||
|
|||
RE: How to sort two lists?
Here is a version that works with your choice of sort (distance) function as a parameter:
Code:
SortByFun has now been added to my collection of sort routines: https://www.hpmuseum.org/forum/thread-11...#pid124491 Ailoher, did you have any specific sort distance function in mind? Or is ABS(LST-Item) OK? Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
12-05-2019, 06:55 PM
Post: #20
|
|||
|
|||
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)