Post Reply 
Best way to sort one list based on another?
07-04-2015, 10:40 AM
Post: #1
Best way to sort one list based on another?
I have two lists

name := { "Bird", "Jordan", "O'Neal" }
height := { 2.06, 1.98, 2.16 }

and I want to sort the names into height order. What's the best way to do this in HP PPL, please?
Find all posts by this user
Quote this message in a reply
07-04-2015, 02:52 PM
Post: #2
RE: Best way to sort one list based on another?
I couldn't find any way to do it except the brute force method:

Code:

EXPORT sortlists(List1,List2)
BEGIN
 local i,j,temp, Size;
 Size:=size(List1);
 if Size <> SIZE(List2) then 
  print("lists are not the same size");
 end;
 for i from 1 to Size do
  for j from 1 to Size−1 do
   if List1(j) < List1(j+1) then
    temp:=List1(j);
    List1(j):= List1(j+1);
    List1(j+1):=temp; 
    temp:=List2(j);
    List2(j):=List2(j+1);
    List2(j+1):=temp;
   end;
  end;
 end;
 return {List1,List2};
END;

   

There is probably a better way, but this at least works.

Road
Find all posts by this user
Quote this message in a reply
07-04-2015, 06:51 PM
Post: #3
RE: Best way to sort one list based on another?
Code:

EXPORT parallel_List_sort()
BEGIN
  local name := { "Bird", "Jordan", "O'Neal" }, height := { 2.06, 1.98, 2.16 };

  for I from 1 to length(height) do
    for J from I+1 to length(height) do
      if height(I) > height(J) then 
        L0:=height(I);L1:=name(I);
        name(I):=name(J); height(I):=height(J);
        name(J):=L1(1);height(J):=L0(1);
      end;
    end;     
  end;

  print();
  print(name);
  print(height);
END;
Find all posts by this user
Quote this message in a reply
07-04-2015, 08:00 PM
Post: #4
RE: Best way to sort one list based on another?
There isn't a PPL built-in function to list in using another list as a key.
As others have suggested one can write your own sort function.

However, the Prime appears to have built-in buttons that can do something very similar for spreadsheet columns and to three columns of statistical data. And it can turn lists into columns.

These built-in features might be of some use if you wanted to avoid writing your own sort function - but they cannot be accessed from the PPL as far as I know.

It seems a pity that spreadsheets and statistical apps can do such a sort, but lists and the PPL cannot (or have I missed something?)

Perhaps this could be added in a future PPL?

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
07-04-2015, 09:53 PM
Post: #5
RE: Best way to sort one list based on another?
Okay, here's a quick & dirty effort[1]:
Code:
EXPORT name := { "Bird", "Jordan", "O'Neal" };
EXPORT height := { 2.06, 1.98, 2.16 };

EXPORT SORTB(a,b)
BEGIN
  LOCAL c, i, p;
  c := EXECON("&2+CHAR(28)+&1", a, b);
  c := SORT(c);
  FOR i FROM 1 TO SIZE(c) DO
    p := INSTRING(c(i), CHAR(28));
    c(i) := MID(c(i), p+1);
  END;
  RETURN c;
END;

Call as: SORTB(name, height)

[1] It's not perfect - two people with the same height will be sorted by name as well.
Find all posts by this user
Quote this message in a reply
07-05-2015, 06:03 AM (This post was last modified: 07-05-2015 06:04 AM by Tyann.)
Post: #6
RE: Best way to sort one list based on another?
Hello
I suggest
Code:

Ecart(n)
BEGIN
LOCAL u;
WHILE u<n DO
 u:=3*u+1;
END;
(u-1)/3;
END;

EXPORT SORTM(o)
BEGIN
 LOCAL f,i,j,d,n;
 LOCAL e,k,l,z,p,s;
 IF TYPE(o[1])==6 THEN
  l:=o[1];f:=1; 
 ELSE
  l:=o; 
 END;
 d:=SIZE(l);
 k:=MAKELIST(I,I,1,d);
 p:=Ecart(d);
 WHILE p>0 DO
  FOR i FROM p+1 TO d DO
   e:={l[i],k[i]}; 
   j:=i;s:=1;
   WHILE j>p  AND s DO
    IF l[j-p]>e[1] THEN
     l[j]:=l[j-p];k[j]:=k[j-p];
     j:=j-p;
    ELSE
     s:=0;
    END;
   END;
   l[j]:=e[1];k[j]:=e[2];
  END; 
  p:=iquo(p,3);
 END;
 IF f THEN
  o[1]:=l;n:=SIZE(o);
  FOR i FROM 2 TO n DO
   z:=SIZE(o[i]);l:={};
   FOR j FROM 1 TO d DO
    l[j]:=o(i,k[j]);
   END;
   o[i]:=IFTE(z>d,CONCAT(l,SUB(o[i],j,z)),l);
  END; 
  o; 
 ELSE
  {l,k};
 END;
END;
SORTM({5,2,3,1}) return {{1,2,3,5},{4,2,3,1}}
SORTM({{5,2,3,1},{100,200,300,400},{"A","B","C","D"}}) return {{1,2,3,5},{400,200,300,100},{"D","B","C","A"}}
SORTM({{5,2,3,1},{100,200,300},{"A","B","C","D"}}) return Erreur
SORTM({{5,2,3,1},{100,200,300,400},{"A","B","C","D","X","H"}}) return {{1,2,3,5},{400,200,300,100},{"D","B","C","A","X","H"}}

Hoping that it will be useful.
Sorry for my English (Google traduction)
Find all posts by this user
Quote this message in a reply
07-05-2015, 07:34 AM
Post: #7
RE: Best way to sort one list based on another? ANSWERED
For those finding this thread in the future, the best answer is here.
Find all posts by this user
Quote this message in a reply
07-05-2015, 06:27 PM
Post: #8
RE: Best way to sort one list based on another?
thank you ,nice response
Find all posts by this user
Quote this message in a reply
Post Reply 




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