lists:sort/2

Asko Husso <>
Tue Jun 3 18:31:12 CEST 2003


Uffe, try:

lists:foldl(
    fun({P,R}, {Low, High}) when P < 3 ->
            {[{P,R}|Low], High};
       (stop, {Low, High}) ->
           lists:keysort(1, Low) ++ lists:keysort(2, High);
       (X, {Low, High}) ->
           {Low, [X|High]}
    end,
    {[], []}, L ++ [stop]).
    
The lists module is nice to have ;-)

/Asko

> X-Authentication-Warning: cbe1066.al.sw.ericsson.se: etxuwig owned process 
doing -bs
> Date: Tue, 3 Jun 2003 18:10:52 +0200 (MEST)
> From: Ulf Wiger <>
> X-X-Sender: 
> To: "Erik Reitsma (ETM)" <>
> cc: 
> Subject: RE: lists:sort/2
> MIME-Version: 1.0
> X-OriginalArrivalTime: 03 Jun 2003 16:12:16.0555 (UTC) 
FILETIME=[E6AADFB0:01C329EA]
> 
> 
> I will try thinking before posting next time.
> Maybe it's time to call it a day.  :)
> 
> /Uffe
> 
> On Tue, 3 Jun 2003, Erik Reitsma (ETM) wrote:
> 
> >> I encountered some surprising results when using
> >> lists:sort/2. Given a list [{P,R}], I wanted a list sorted
> >> on P for all P < 3, and sorted on R for the remainder.
> >
> >What result would you like when you compare {P1,R1} and {P2,R2}, and P1<3 and 
P2>=3? And what if P1>=3 and P2<3? It looks like you have not specified this.
> >
> >> lists:sort(
> >>   fun({P1,_}, {P2,_}) when P1 < 3 ->
> >> 	  P1 < P2;
> >>      ({_,R1}, {_,R2}) ->
> >> 	  R1 < R2
> >>   end, L).
> >
> >If this function even anti-symmetrical? Consider X1={1,10} and X2={4,9}. Then 
F(X1,X2) = true and F(X2,X1) = true. Your function should be such, that F(A,B) = 
not F(B,A) for all A and B.
> >
> >When you have defined the relation you want, I think you will be able to find 
a fun that really implements that relation.
> >
> >*Erik.
> >
> 
> -- 
> Ulf Wiger, Senior Specialist,
>    / / /   Architecture & Design of Carrier-Class Software
>   / / /    Strategic Product & System Management
>  / / /     Ericsson AB, Connectivity and Control Nodes
> 

Asko Husso                       	E-mail:
Ericsson AB		  	        Phone: +46 8 7192324
Varuvägen 9B                               
S-126 25 Stockholm-Älvsjö, Sweden





More information about the erlang-questions mailing list