lists:sort/2

Erik Reitsma (ETM) Erik.Reitsma@REDACTED
Tue Jun 3 17:59:18 CEST 2003


> 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.




More information about the erlang-questions mailing list