Custom sort?
|
01-20-2019, 06:09 PM
Post: #1
|
|||
|
|||
Custom sort?
Hi! I need to sort lists of lists according to their second element, i.e. an input {{1, 2}, {2, 2.5}, {3, 1.5}} should result in an output {{2, 2.5}, {1, 2}, {3, 1.5}}.
The first element of the sublists can be arbitrary (e.g. strings) and the second elements of the sublists are non-unique numbers, so they cannot be used for reverse indexing. Is there a custom sort command for this on the HP Prime? |
|||
01-20-2019, 06:50 PM
Post: #2
|
|||
|
|||
RE: Custom sort?
maybe it helps http://www.hpmuseum.org/forum/thread-113...light=sort
I was unable to find it again with google! I had to use the internal search that doesn't work bad if one direct it on very specific lines. Wikis are great, Contribute :) |
|||
01-20-2019, 08:20 PM
Post: #3
|
|||
|
|||
RE: Custom sort?
(01-20-2019 06:09 PM)EricR Wrote: Hi! I need to sort lists of lists according to their second element, i.e. an input {{1, 2}, {2, 2.5}, {3, 1.5}} should result in an output {{2, 2.5}, {1, 2}, {3, 1.5}}. SORT({{1,2},{2,2.5},{3,1.5}},2) will return {{3,1.5},{1,2},{2,2.5}}. If you want the second elements in descending order, just reverse the list: REVERSE(SORT({{1,2},{2,2.5},{3,1.5}},2)) will return {{2,2.5},{1,2},{3,1.5}} |
|||
01-20-2019, 09:13 PM
Post: #4
|
|||
|
|||
RE: Custom sort?
Didier,
The [sort _by] identifier works great in [Home] but not in [CAS], (Emulator): [HOME] SORT({{1,2},{2,2.5},{3,1.5}},2) ==> {{3,1.5},{1,2},{2,2.5}} [Cas] SORT({{1,2},{2,2.5},{3,1.5}},2) ==> {{3,1.5},{2,2.5},{1,2}} -Dale- |
|||
01-20-2019, 11:38 PM
(This post was last modified: 01-20-2019 11:41 PM by Didier Lachieze.)
Post: #5
|
|||
|
|||
RE: Custom sort?
The CAS sort function works differently than the HOME SORT function for the second argument, it should define a function to be used for the sorting.
In CAS : sort({{1,2},{2,2.5},{3,1.5}},(x,y)->when(x[2] = y[2],x[1]>y[1],x[2]>y[2])) returns: {{2,2.5},{1,2},{3,1.5}} The sorting is done on the second item in descending order, and if the second item is the same, on the first item also in descending order. |
|||
01-21-2019, 03:05 AM
(This post was last modified: 01-21-2019 03:13 AM by Albert Chan.)
Post: #6
|
|||
|
|||
RE: Custom sort?
(01-20-2019 11:38 PM)Didier Lachieze Wrote: In CAS : Another way is Schwartian transformed sort. For above case, assuming 1-based indexing, in X-Cas: m := [[1,2], [2,2.5], [3,1.5]] reverse(swapcol(sort(swapcol(m, 1,2)), 1,2)) --> [[2,2.5], [1,2], [3,1.5]] |
|||
01-21-2019, 10:56 AM
Post: #7
|
|||
|
|||
RE: Custom sort?
(01-20-2019 11:38 PM)Didier Lachieze Wrote: The CAS sort function works differently than the HOME SORT function for the second argument, it should define a function to be used for the sorting. With only the provided hp documentation, using a function as the second argument isn't shown. In the original example, if sort() is given as the [CAS]command, it gets case-changed to SORT(), which doesn't work when the format used follows either the User Guide, or the on screen help guidance. For the CAS side, perhaps SORT({{1,2},{2,2.5},{3,1.5}},2), should just return, "Error: Bad argument error." Another approach could be to suggest: See XCAS help, (an external resource). This idea connects the CAS side with XCAS, and hints that the XCAS help could be useful for this, and perhaps other CAS needs. |
|||
01-22-2019, 04:23 PM
Post: #8
|
|||
|
|||
RE: Custom sort?
(01-21-2019 03:05 AM)Albert Chan Wrote: Another way is Schwartian transformed sort. Interesting. I have been using that method for years but never new it had a name. |
|||
01-22-2019, 05:34 PM
Post: #9
|
|||
|
|||
RE: Custom sort?
(01-20-2019 11:38 PM)Didier Lachieze Wrote: In CAS : Playing with the key compare function, noticed an odd behavior. X-Cas logical operators (and, or) have the same precedence. a and b or c => (a and b) or c a or b and c => (a or b) and c So, above compare function using logical operator need an extra parenthesis: (x,y) -> x[2]>y[2] or (x[2]==y[2] and x[1]>y[1]) |
|||
01-22-2019, 06:54 PM
Post: #10
|
|||
|
|||
RE: Custom sort?
Hi!, all :
In the site of Eric Rechlin, have ... 1) https://www.hpcalc.org/historical/listapi-1.2.zip 2) https://www.hpcalc.org/details/8896 |
|||
01-23-2019, 01:53 PM
(This post was last modified: 01-23-2019 02:49 PM by StephenG1CMZ.)
Post: #11
|
|||
|
|||
RE: Custom sort?
(01-22-2019 06:54 PM)informach Wrote: Hi!, all : The latest versions of those programs can be found here: SortL V0.4C: http://www.hpmuseum.org/forum/thread-11365.html ListAPI V1.6:: http://www.hpmuseum.org/forum/thread-9411-page-2.html Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)