Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

re: [Q] Partial sorting?

3 views
Skip to first unread message

WJ

unread,
Jul 24, 2016, 5:33:53 PM7/24/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Kent M. Pitman wrote:

> Sungwoo Lim <sung...@cad.strath.ac.uk> writes:
>
> > Hello,
> >
> > I have an array which elements are LIST as below.
> >
> > #((1 3 5) (5 3 8) (2 7 1) (3 7 4) (9 1 3)) ; let's call it ARRAY
> >
> > I would like to sort the array by second element of each list.
> >
> > (stable-sort array #'> :key #'cadr))
> >
> > So, now I can sort the array as
> >
> > #((2 7 1) (3 7 4) (1 3 5) (5 3 8) (9 1 3))
> >
> > However, I have two seven and three respectively in the second element,
> > so I want to sort it by the first element.
> > Desired result should be
> >
> > #((3 7 4) (2 7 1) (5 3 8) (1 3 5) (9 1 3))
> >
> > Is there any way to sort partially as above?
> > I can't find any example from CL references.
> > Thanks alot.
> >
>
> (stable-sort (sort array #'> :key #'car) #'> :key #'cadr)
>
> That is, first sort by the secondary key.
> Then, do a stable-sort which preserves that ordering in the equal cases
> but perturbs things in the case of a dominating primary key.

OCaml:

List.sort
(fun [a;b;c] [d;e;f] -> compare [e;f] [b;a])
[[1;3;5];[5;3;8];[2;7;1];[3;7;4];[9;1;3]] ;;

[[3; 7; 4]; [2; 7; 1]; [5; 3; 8]; [1; 3; 5]; [9; 1; 3]]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunkers-5.html
https://www.youtube.com/watch?v=EEl_1HWFRfo
0 new messages