MathGroup Archive 2011

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

Search the Archive

Re: Sorting on mulitple criteria

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115549] Re: Sorting on mulitple criteria
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Fri, 14 Jan 2011 06:18:34 -0500 (EST)

Hi Frank,

you can use a more complex comparison function which incorporates both of
your criteria:

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}}

In[11]:= Sort[data,  First@#1 > First@#2 || (First@#1 == First@#2 && #1[[3]]
> #2[[3]]) &]

Out[11]= {{3, 3, 3}, {3, 2, 3}, {3, 1, 3}, {3, 3, 2}, {3, 2, 2}, {3,
  1, 2}, {3, 3, 1}, {3, 2, 1}, {3, 1, 1}, {2, 3, 3}, {2, 2, 3}, {2, 1,
   3}, {2, 3, 2}, {2, 2, 2}, {2, 1, 2}, {2, 3, 1}, {2, 2, 1}, {2, 1,
  1}, {1, 3, 3}, {1, 2, 3}, {1, 1, 3}, {1, 3, 2}, {1, 2, 2}, {1, 1,
  2}, {1, 3, 1}, {1, 2, 1}, {1, 1, 1}}

If you want to automate this process for several sorting criteria applied
one after
another, this is also possible but you will need to impose more stringent
requirements on
your comparison functions. They generally should reflect some partial order
and return
-1,0 or 1 (3 different values) rather than just True or False.

Here is a function which automates this:

Clear[sortMultiple];
sortMultiple[toSort_List, criteria : {__}] :=
  Sort[toSort,
   Fold[Function[{cmp1, cmp2},
     cmp1[#1, #2] == -1 || (cmp1[#1, #2] == 0 &&
         cmp2[#1, #2] == -1) &], First@criteria, Rest@criteria]];

In our case, our comparison functions can be implemented as

comp1 = Sign[#2[[1]] - #1[[1]]] &;
comp2 = Sign[#2[[3]] - #1[[3]]] &;

We get the same answer as before, of course:

In[24]:= sortMultiple[data, {comp1, comp2}]

Out[24]= {{3, 3, 3}, {3, 2, 3}, {3, 1, 3}, {3, 3, 2}, {3, 2, 2}, {3,
  1, 2}, {3, 3, 1}, {3, 2, 1}, {3, 1, 1}, {2, 3, 3}, {2, 2, 3}, {2, 1,
   3}, {2, 3, 2}, {2, 2, 2}, {2, 1, 2}, {2, 3, 1}, {2, 2, 1}, {2, 1,
  1}, {1, 3, 3}, {1, 2, 3}, {1, 1, 3}, {1, 3, 2}, {1, 2, 2}, {1, 1,
  2}, {1, 3, 1}, {1, 2, 1}, {1, 1, 1}}

Hope this helps.

Regards,
Leonid


On Thu, Jan 13, 2011 at 11:31 AM, Frank Letkiewicz <
Frank.Letkiewicz at cadmusgroup.com> 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}}
>
>
>



  • Prev by Date: Re: Styles for inline boxes
  • Next by Date: Re: FindInstance for sum of primes
  • Previous by thread: Re: Sorting on mulitple criteria
  • Next by thread: MathLink with Mathematica Home Edition 8.0