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

Sorting (again!), but with multiple columns

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50417] Sorting (again!), but with multiple columns
  • From: "DIAMOND Mark R." <dot at dot.dot>
  • Date: Thu, 2 Sep 2004 04:35:08 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

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





  • Prev by Date: Re: writing a value of an expression defined in a delayed module to a file.
  • Next by Date: Re: Beware of NSolve - nastier example
  • Previous by thread: Re: writing a value of an expression defined in a delayed module to a file.
  • Next by thread: Re: Sorting (again!), but with multiple columns