From: Jeremy Rowlings
Subject: list sorting problem with two constraints
Date: 
Message-ID: <8vg7o5$d5h4$1@spnode25.nerdc.ufl.edu>
how can i sort a list in the following manner?

i want to sort the list based on second member of sublist, but it shouldn't
change the
existing first member sequence..

( (3 5) (3 1) (3 3) (1 2) (1 7) (1 1) (2 3) )

to
( (3 1) (3 3) (3 5) (1 1) (1 2) (1 7) (2 3) )

thanks

From: Jochen Schmidt
Subject: Re: list sorting problem with two constraints
Date: 
Message-ID: <8vggbs$4bccm$1@ID-22205.news.dfncis.de>
Jeremy Rowlings wrote:

> how can i sort a list in the following manner?
> 
> i want to sort the list based on second member of sublist, but it
> shouldn't change the
> existing first member sequence..
> 
> ( (3 5) (3 1) (3 3) (1 2) (1 7) (1 1) (2 3) )
> 
> to
> ( (3 1) (3 3) (3 5) (1 1) (1 2) (1 7) (2 3) )
> 
> thanks

Try this:

(defvar *a-list* ( (3 5) (3 1) (3 3) (1 2) (1 7) (1 1) (2 3) ))

(sort (copy-seq *a-list*)
    #'(lambda (left right)
        (if (= (first left)
                (first right))
            (< (second left)
                (second right))
           nil)))


I hope this was not a excercise!!!

Regards,
Jochen Schmidt
From: Anders Vinjar
Subject: Re: list sorting problem with two constraints
Date: 
Message-ID: <copaeasduim.fsf@john.uio.no>
>>> "JR" == Jeremy Rowlings <······@hotmail.com> writes:

    JR> how can i sort a list in the following manner?  i want to
    JR> sort the list based on second member of sublist, but it
    JR> shouldn't change the existing first member sequence..

Tell me, arent people where you live requested to solve the
homework they are given by themselves?

(The solution to your problem is not difficult, and any text on
lisp includes numerous examples like the one you came up with.)
From: Jason Trenouth
Subject: Re: list sorting problem with two constraints
Date: 
Message-ID: <uugn1ts3jse2jvg5du1h0ce21pc596tipl@4ax.com>
On Wed, 22 Nov 2000 05:40:38 -0500, "Jeremy Rowlings" <······@hotmail.com>
wrote:

> how can i sort a list in the following manner?
> 
> i want to sort the list based on second member of sublist, but it shouldn't
> change the
> existing first member sequence..
> 
> ( (3 5) (3 1) (3 3) (1 2) (1 7) (1 1) (2 3) )
> 
> to
> ( (3 1) (3 3) (3 5) (1 1) (1 2) (1 7) (2 3) )

Is this a question from a Lisp class?

Try expressing the actual specification in Lisp and then think about how to
make that more efficient. i.e. assume you are going to call sort or stable-sort
and then decide what the predicate argument has to be. IF the FIRST elements
are = then you need to test the SECOND elements with respect to <. Otherwise if
the FIRST elements are not = then you need to order them with respect to their
POSITION in the argument list. Etc Etc.

__Jason
From: Jeremy Rowlings
Subject: Re: list sorting problem with two constraints
Date: 
Message-ID: <8vgob3$5acq$1@spnode25.nerdc.ufl.edu>
well, it may not be hard for you Anders but it is for me.
and well, i'm a student but not a cs student
and this is not an exercise..so thanks for help

i just wanted to learn if there is a shortcut to do this with using sort
function
rather than writing a code..I couldn't figure out how to use sort keys and
predicates
if a write a code i'm sure there are hundreds of ways..
but just with using sort?
From: Anders Vinjar
Subject: Re: list sorting problem with two constraints
Date: 
Message-ID: <copzoirumu1.fsf@clara.uio.no>
>>> "JR" == Jeremy Rowlings <······@hotmail.com> writes:

    JR> i just wanted to learn if there is a shortcut to do this
    JR> with using sort function rather than writing a code..

You can use #'sort with a suitable key.  Depending on what counts
as "writing a code", the following specification could do it:
provide sort with a predicate taking two lists as arguments,
returning t if each lists car is the same number and the cadr of
the first list is less then the cadr of the second list.

-anders
From: Wolfhard Buß
Subject: Re: list sorting problem with two constraints
Date: 
Message-ID: <m366lewho3.fsf@buss-14250.user.cis.dfn.de>
Anders Vinjar <········@notam.uio.no> writes:

> You can use #'sort with a suitable key.

> provide sort with a predicate taking two lists as arguments,
> returning t if each lists car is the same number and the cadr of
> the first list is less then the cadr of the second list.

This predicate doesn't represent a total order on (integer integer).
Sort with your predicate yields the expected value by accident.
Use stable-sort with the proposed predicate or ask Marco Antoniotti
for the recipe.

Regards,
Wolfhard.

-- 
"Das Auto hat keine Zukunft. Ich setze aufs Pferd."  Wilhelm II. (1859-1941)
From: Marco Antoniotti
Subject: Re: list sorting problem with two constraints
Date: 
Message-ID: <y6caeapwciu.fsf@octagon.mrl.nyu.edu>
·····@gmx.net (Wolfhard Bu�) writes:

> Anders Vinjar <········@notam.uio.no> writes:
> 
> > You can use #'sort with a suitable key.
> 
> > provide sort with a predicate taking two lists as arguments,
> > returning t if each lists car is the same number and the cadr of
> > the first list is less then the cadr of the second list.
> 
> This predicate doesn't represent a total order on (integer integer).
> Sort with your predicate yields the expected value by accident.
> Use stable-sort with the proposed predicate or ask Marco Antoniotti
> for the recipe.

Which recipe?  Carbonara or Paniscia? :)

Cheers

-- 
Marco Antoniotti =============================================================
NYU Bioinformatics Group			 tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                          fax  +1 - 212 - 995 4122
New York, NY 10003, USA				 http://galt.mrl.nyu.edu/valis
             Like DNA, such a language [Lisp] does not go out of style.
			      Paul Graham, ANSI Common Lisp
From: Marco Antoniotti
Subject: Re: list sorting problem with two constraints
Date: 
Message-ID: <y6chf50c7t6.fsf@octagon.mrl.nyu.edu>
"Jeremy Rowlings" <······@hotmail.com> writes:

> how can i sort a list in the following manner?
> 
> i want to sort the list based on second member of sublist, but it shouldn't
> change the
> existing first member sequence..
> 
> ( (3 5) (3 1) (3 3) (1 2) (1 7) (1 1) (2 3) )
> 
> to
> ( (3 1) (3 3) (3 5) (1 1) (1 2) (1 7) (2 3) )
> 

Your problem formulation is not quite right.  You want to sort the
subgroups of the runs of couples with the same first element.
So your problem is

	1 - split the sequence according to the first element
	2 - sort the subsequences
	3 - glue them together.

Piece of cake :)

Cheers

-- 
Marco Antoniotti =============================================================
NYU Bioinformatics Group			 tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                          fax  +1 - 212 - 995 4122
New York, NY 10003, USA				 http://galt.mrl.nyu.edu/valis
             Like DNA, such a language [Lisp] does not go out of style.
			      Paul Graham, ANSI Common Lisp