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