Post Reply 
Best way to sort one list based on another?
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
Post Reply 


Messages In This Thread
RE: Best way to sort one list based on another? - Tyann - 07-05-2015 06:03 AM



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