Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Sorting (again!), but with multiple columns

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50434] Re: [mg50417] Sorting (again!), but with multiple columns
  • From: DrBob <drbob at bigfoot.com>
  • Date: Fri, 3 Sep 2004 03:35:50 -0400 (EDT)
  • References: <200409020835.EAA02137@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

Here's a sampling routine:

sample[n_]:=RandomArray[BinomialDistribution[12,.23],{n,3}]
sample[10]

{{2,3,3},{1,4,1},{1,4,7},{2,4,4},{2,2,4},{1,4,3},{2,5,4},{1,1,1},{
   4,3,2},{2,3,2}}

For your first sort:

Clear@sort
sort[{_, a_, c_}, {_, b_, d_}] := a < b || a == b && c >= d
data = sample[10]
Sort[data, sort]

{{2,2,0},{2,3,2},{3,2,5},{2,1,4},{2,4,2},{2,2,3},{1,1,3},{2,2,4},{
   1,4,3},{0,3,1}}

{{2,1,4},{1,1,3},{3,2,5},{2,2,4},{2,2,3},{2,2,0},{2,3,2},{0,3,1},{
   1,4,3},{2,4,2}}

For your second sort:

Clear@sort
sort[{_, a_, c_}, {_, b_, d_}] := a < b || a == b && (EvenQ[a] && c <= d || OddQ[a] && c >= d)
data = sample[10]
Sort[data, sort]

{{4,2,3},{4,2,1},{4,2,4},{4,4,3},{4,5,2},{5,4,2},{3,1,1},{2,2,3},{
   3,3,4},{2,2,1}}

{{3,1,1},{4,2,1},{2,2,1},{4,2,3},{2,2,3},{4,2,4},{3,3,4},{5,4,2},{
   4,4,3},{4,5,2}}

If all second column numbers are Integers (as in my example), or if you want to treat non-Integers the same as odds, the sort function could be

Clear@sort
sort[{_, a_, c_}, {_, b_, d_}] := a < b || a == b && (EvenQ[a] && c <= d || c >= d)

If the second column has any non-Integer values, the result may be strange, since then neither EvenQ nor OddQ will test true. But you could flip a coin in that case:

Clear@sort
sort[{_, a_, c_}, {_, b_, d_}] := a < b || a == b && (EvenQ[a] && c <= d || OddQ[a] && c >= d ||
   Random[] <= 1/2)

Bobby

On Thu, 2 Sep 2004 04:35:08 -0400 (EDT), DIAMOND Mark R. <dot at dot.dot> wrote:

> I have read the long 2002 posting on sorting a matrix by column, and I am
> also familiar with Ted Erseks very fast method using Rotate ... but I still
> can find quite what I want. I have two related sorting problems ... related
> in that they both require sorting by multiple columns, and the second
> problems is a kind of subset of the first.
>
> First, I want to sort a (100000 x 3) matrix so that it is sorted firstly on
> column 2 (ascending order) and then subsorted on column 3 (decending order).
> I had thought incorrectly, that the evaluation order of And[] would lead the
> following to work ... I've written the predicate out separately just to make
> it clearer.
>
> tQ23[t1_, t2_] :=
>   OrderedQ[{t1[[2]], t2[[2]]}] \[And] OrderedQ[{t2[[3]], t1[[3]]}]
>
> t = Table[{i, j, k}, {i, 2}, {j, 2}, {k, 2}];
> Sort[t, tQ23]
>
> {{1, 1, 2}, {2, 1, 2}, {1, 2, 2}, {2, 2, 2}, {1, 1, 1}, {2, 1, 1}, {1, 2,
>     1}, {2, 2, 1}}
>
> Second, I want to sort a (100000 x 3) matrix so that it is soted first on
> column 2 (ascending order), then *conditionally* subsorted on column 3, with
> ascending order if column 2 is Even, and decending if column 2 is Odd. I
> have no idea how to approach this, except by sorting on column 2, stripping
> out blocks of rows, and resorting them before appending them to a new list.
> This is horribly slow. Any suggestions? Any speed-up, or flash of insight
> would be most appreciated.
>
> On a different note, given the ubiquity of sorting, I would have thought
> that this is an area where a Wolfram time-investment would really pay off.
> In the late 70s I used to use a CDC-6600 machine that had a superb sorting
> utility where the sort order, sub-order, and various conditionals were easy
> to specify and was blindingly fast, at least on straight numeric or
> character data.
>
> --
> Mark R. Diamond
>
>
>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Re: Beware of NSolve - nastier example
  • Next by Date: Re: Re: newbie is looking
  • Previous by thread: Re: Sorting (again!), but with multiple columns
  • Next by thread: Martix reconstruction by tensors of nonlinearity in an interger sequence