Re: Sorting of pairs
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Sorting of pairs
- From: John Lee <lee at math.washington.edu>
- Date: Fri, 7 Aug 92 08:30:31 -0700
Riccardo Rigon <RIGON at iveuncc.bitnet> writes:
> Dear mathgroupers, I have a list of pairs:
> list={{2,3},{4,5},{1,3},{3,8},{4,4}}
> and I want to order them with this rule:
> rule[a_List,b_List]:=a/;(a[[1]]< b[[1]] || ( a[[1]]==b[[1]] && a[[2]]<b[[2]]))
> rule[a_List,b_List]:=b
> The way to do this should be:
> Sort[list,rule]
> Sort[list,rule[#1,#2]&]
> but no one of the two methods works.
> Does anybody knows the reason?
> Thank you in advance.
First of all, the second argument to Sort is supposed to be a True/False
function: it must return True if the first argument should come earlier in
the sort, and False otherwise. So your two definitions for rule should be
replaced by:
rule[a_List,b_List]:=(a[[1]]< b[[1]] ||
(a[[1]]==b[[1]] && a[[2]]<b[[2]]))
However, this is just Mathematica's usual ordering rule for lists, so the
result will be the same if you simply use Sort[list].
Jack Lee
Dept. of Mathematics
University of Washington
Seattle, WA