Re: A little help with sort
- To: mathgroup at smc.vnet.net
- Subject: [mg112511] Re: A little help with sort
- From: James Stein <mathgroup at stein.org>
- Date: Sat, 18 Sep 2010 07:24:30 -0400 (EDT)
I think the following will solve your problem. order: needs no explanation. sort124: sorts as you desire: by column 1; then by column2; then reversed by column 4. sort: generalizes this, allowing you to specify a sorting "scheme" Clear[order, sort124, sort, scheme]; order[a_, b_, i_] := Order[a[[i]], b[[i]]]; sort124[a_, b_] := Module[{o}, If[(o = order[a, b, 1]) == 0, If[(o = order[a, b, 2]) == 0, o = -order[a, b, 4] ] ]; o > 0 ]; scheme = {1, 2, -4}; sort[a_, b_] := Module[{o, s = 0, i}, While[++s <= Length[scheme], i = scheme[[s]]; If[i > 0, If[0 != (o = order[a, b, i]), Return[o >= 0]], If[0 != (o = order[a, b, -i]), Return[o <= 0]] ]]; False ]; On Fri, Sep 17, 2010 at 3:42 AM, Blackwell, Keith <Keith.Blackwell at rbccm.com > wrote: > The problem I am having: > > Let's say I have a 200 x 200 matrix. > > I want to sort it in order of the values in column one. > > Then I want it to sort all the doubles by the values in column two. > > Then I want it to sort all the double doubles by reverse of the values > of column 4 or whatever. > > Is there an easy way to do this. I know I can write something long and > annoying to do it, but I'm getting to the point in my use of mathematica > where I want to write more concise code. > > Sort[Matrix, #1[[1]]>#2[[1]]&] seems to totally negate any pre > sorting... I tried to fold it backwards starting from column 4 and it > seems that sort just ignores whatever I do then. > > Thanks in advance, > > Keith > ________________________________________ > > This E-Mail (including any attachments) may contain privileged or > confident= > ial information. It is intended only for the addressee(s) indicated above. > > The sender does not waive any of its rights, privileges or other > protection= > s respecting this information. > > Any distribution, copying or other use of this E-Mail or the information > it= > contains, by other than an intended recipient, is not sanctioned and is > pr= > ohibited. > > If you received this E-Mail in error, please delete it and advise the > sende= > r (by return E-Mail or otherwise) immediately. > > This E-Mail (including any attachments) has been scanned for viruses. > > It is believed to be free of any virus or other defect that might affect > an= > y computer system into which it is received and opened. > > However, it is the responsibility of the recipient to ensure that it is > vir= > us free. > > The sender accepts no responsibility for any loss or damage arising in any > = > way from its use. > > E-Mail received by or sent from RBC Capital Markets is subject to review > by= > Supervisory personnel. > > Such communications are retained and may be produced to regulatory > authorit= > ies or others with legal rights to the information. > > IRS CIRCULAR 230 NOTICE: TO COMPLY WITH U.S. TREASURY REGULATIONS, WE > ADVI= > SE YOU THAT ANY U.S. FEDERAL TAX ADVICE INCLUDED IN THIS COMMUNICATION IS > N= > OT INTENDED OR WRITTEN TO BE USED, AND CANNOT BE USED, TO AVOID ANY U.S. > FE= > DERAL TAX PENALTIES OR TO PROMOTE, MARKET, OR RECOMMEND TO ANOTHER PARTY > AN= > Y TRANSACTION OR MATTER. > >