Bill Allombert on Sun, 15 Jun 2003 18:58:23 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Sorting multi-dimensional vectors


On Sun, Jun 15, 2003 at 05:47:16PM +0100, Jon Perry wrote:
> I wish to sort a vector, e.g.
> 
> [[1,2],[2,3],[1,3]]
> 
> All the inner vectors are the same length, and the order returned should be;
> 
> [[1,2],[1,3],[2,3]]

I suppose this is lexicographic order ?

> Is there a routine for this, vecsort() doesn't seem to work.

vecsort works fine, you just need to use the flag 2:

? ?vecsort
vecsort(x,{k},{flag=0}): sorts the vector of vectors (or matrix) x in
ascending order, according to the value of its k-th component if k is not
omitted. Binary digits of flag (if present) mean: 1: indirect sorting, return
the permutation instead of the permuted vector, 2: sort using lexicographic
order, 4: use descending instead of ascending order.

? vecsort([[1,2],[2,3],[1,3]],2)
%1 = [[1, 2], [1, 3], [2, 3]]

Cheers,
Bill.