RE: newbie question on sorting
- To: mathgroup at smc.vnet.net
- Subject: [mg26456] RE: [mg26420] newbie question on sorting
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 21 Dec 2000 01:51:57 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Niels,
Actually Mathematica will lexicographically sort the rows of a matrix. So
the rows will be sorted first of the first column, then in case of ties on
the second column, then on the third column etc.
mat = {{3, 2, 1}, {2, -1, 5}, {2, -1, 3}, {-2, 0, 0}};
Sort[mat]
{{-2, 0, 0}, {2, -1, 3}, {2, -1, 5}, {3, 2, 1}}
If you want to sort only on the first column, and leave other columns
unsorted you could specify a specific sorting function using a pure
function:
Sort[mat, First[#1] <= First[#2] &]
{{-2, 0, 0}, {2, -1, 5}, {2, -1, 3}, {3, 2, 1}}
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
> From: niels Waller [mailto:niels.waller at home.com]
To: mathgroup at smc.vnet.net
>
> Dear mathgroup readers,
>
> I have just started using Mathematica for a programming task and have run
> into a difficulty. I have a matrix M. I would like to sort the rows of M
> based on the values of the first column. Is there any easy way
> to do this?
> Any and all help will be greatly appreciated.
>
> Thanks,
>
>
> Niels
>
> Please send suggestions to: niels.waller at vanderbilt.edu
>
>
> *******************************************************
> Niels Waller, Ph.D.
> Director of Quantitative Methods and Evaluation
> Department of Psychology and Human Development
> Vanderbilt University
> Phone: (615) 322-8380 email: niels.waller at vanderbilt.edu
> QME Home page: http://www.vanderbilt.edu/quantmetheval/
> *******************************************************