MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Sorting on mulitple criteria

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115557] Re: Sorting on mulitple criteria
  • From: Peter Pein <petsie at dordos.net>
  • Date: Fri, 14 Jan 2011 06:20:07 -0500 (EST)
  • References: <igmd7t$kg$1@smc.vnet.net>

On 13.01.2011 09:30, Frank Letkiewicz wrote:
> What is the easiest way to sort a large data file using multiple criteria?
>
> For example, if I had the following data set:
>
> [cid:image002.png at 01CBB22A.226D2560]
>
> [contact the author to get this - Moderator]
>
> How would I sort it so that it is largest to smallest based on the first value in each element, and within that it is largest to smallest based on the third value in each element so that the output is:
>
> [cid:image004.png at 01CBB22A.226D2560]
>
> [contact the author to get this - Moderator]
>
> I know I can use the following to get the sort right on the first value, but how do I incorporate the second sort criterion?
>
> Sort[data,#1[[1]]>#2[[1]]&]
>
> Thanks in advance,
>
> Frank
>
> data={{1,1,1},{1,1,2},{1,1,3},{1,2,1},{1,2,2},{1,2,3},{1,3,1},{1,3,2},{1,3,3},{2,1,1},{2,1,2},{2,1,3},{2,2,1},{2,2,2},{2,2,3},{2,3,1},{2,3,2},{2,3,3},{3,1,1},{3,1,2},{3,1,3},{3,2,1},{3,2,2},{3,2,3},{3,3,1},{3,3,2},{3,3,3}}
>
>

use SortBy:

In[6]:= data=Tuples[{1,2,3},{3}];
SortBy[data,-#[[{1,3}]]&]
Out[7]= 
{{3,1,3},{3,2,3},{3,3,3},{3,1,2},{3,2,2},{3,3,2},{3,1,1},{3,2,1},{3,3,1},{2,1,3},{2,2,3},{2,3,3},{2,1,2},{2,2,2},{2,3,2},{2,1,1},{2,2,1},{2,3,1},{1,1,3},{1,2,3},{1,3,3},{1,1,2},{1,2,2},{1,3,2},{1,1,1},{1,2,1},{1,3,1}}

Peter



  • Prev by Date: Re: Units question
  • Next by Date: Re: Styles for inline boxes
  • Previous by thread: Sorting on mulitple criteria
  • Next by thread: Re: Sorting on mulitple criteria